*******************************************************************************

************         Resource discoveries, FDI bonanzas, and    ***************
************       local multipliers: Evidence from Mozambique     ************
 
                     * Gerhard Toews and Pierre-Louis Vézina *

*******************************************************************************

*SET DIRECTORY
*cd ""

use  fdi_restat_figures, clear
*the file with real country names --> fdi.dta 


*******************************************************************************
*******************************************************************************
*******************************************************************************
*******************************************************************************

*********** CROSS COUNTRY RESULTS ***************

*******************************************************************************
*******************************************************************************
*******************************************************************************
*******************************************************************************


****************************************************
****************************************************
*SUM TABLES AND GRAPHS
****************************************************
****************************************************

****************************************************
****************************************************
*FIGURE 1
*which country discovers and how big is it, in relative terms
*figure in paper has country labels which for data secutrity purposes are randomly reassgined here
****************************************************
****************************************************

twoway ///
(scatter  size year if GEO=="Africa",  mlabel(iso) mlabsize(vsmall) mlabpos(9)  ) ///
(scatter  size year if GEO=="Asia",  mlabel(iso) mlabsize(vsmall) mlabpos(9) ) ///
(scatter  size year if GEO=="Europe",  mlabel(iso) mlabsize(vsmall) mlabpos(9) ) ///
(scatter  size year if GEO=="Americas",  mlabel(iso) mlabsize(vsmall) mlabpos(9) ) ///
(scatter  size year if GEO=="Middle East",  mlabel(iso) mlabsize(vsmall) mlabpos(9) ) ///
if discovery==1 & year>2002, yscale(log) ylabel(0 1 3 10 40 100 500 1500) xtitle("") scheme(plotplainblind) legend(off)  
graph export discoveries_.pdf, as(pdf)  replace

****************************************************
****************************************************

****************************************************
****************************************************
*FIGURE 2 is a map done with QGIS: not reproduce here since this data is not used in the regressions
****************************************************
****************************************************

****************************************************
****************************************************
*FIGURE 7
*showing the raw data for the only 4 african countries, which experienced a giant discovery within our sample period
****************************************************
****************************************************

preserve
gen year_d=year if discovery==1
replace year_d=. if year_d<2006
replace year_d=. if year_d>2011
bys iso: egen min_year_d=min(year_d)
*create the periods relative to the discovery
gen time=year-min_year_d
keep if time!=.
keep time iso  year Total_FDI non_extraction_FDI Total_FDI_projects non_extraction_projects nb_source_countries nb_subsectors non_extraction_jobs Total_jobs nb_cities
*create normalized measures: relative to the period of discovery
foreach x in Total_FDI non_extraction_FDI Total_FDI_projects non_extraction_projects nb_source_countries nb_subsectors non_extraction_jobs Total_jobs  nb_cities{
gen zero`x'=`x' if time==0
bys iso: egen zero_d_`x'=max(zero`x')
gen n`x'=(`x'/zero_d_`x')*100
}
collapse n* Total_jobs, by(time iso)
encode iso, gen(id)
xtset id time
lab var nnb_subsectors "Subsectors"
lab var  nnon_extraction_projects "Non-extractive projects"
lab var  nnon_extraction_jobs "Non-extractive jobs"
lab var nnb_cities "Destination cities"
foreach x in  GHA ETH {
tsline   nnon_extraction_projects nnon_extraction_jobs nnb_subsectors nnb_cities  if time>-6 & time<6 &iso=="`x'", xlabel(-5(5)5) ylabel(0(500)1500)  scheme(Plotplainblind)  xtitle(Discovery in year 0) xline(0) title(`x') legend(ring(0) position(11) bmargin(small)) xsize(5) ysize(5)  ylabel(,nogrid) xlabel(,nogrid)
graph save `x'boom_.gph,  replace
graph export "`x'boom_.pdf", as(pdf)  replace
}
restore




preserve
gen year_d=year if discovery==1
replace year_d=. if year_d<2006
replace year_d=. if year_d>2011
bys iso: egen min_year_d=min(year_d)
*create the periods relative to the discovery
gen time=year-min_year_d
keep if time!=.
keep time iso  year Total_FDI non_extraction_FDI Total_FDI_projects non_extraction_projects nb_source_countries nb_subsectors non_extraction_jobs Total_jobs nb_cities
*create normalized measures: relative to the period of discovery
foreach x in Total_FDI non_extraction_FDI Total_FDI_projects non_extraction_projects nb_source_countries nb_subsectors non_extraction_jobs Total_jobs  nb_cities{
gen zero`x'=`x' if time==0
bys iso: egen zero_d_`x'=max(zero`x')
gen n`x'=(`x'/zero_d_`x')*100
}
collapse n* Total_jobs, by(time iso)
encode iso, gen(id)
xtset id time
lab var nnb_subsectors "Subsectors"
lab var  nnon_extraction_projects "Non-extractive projects"
lab var  nnon_extraction_jobs "Non-extractive jobs"
lab var nnb_cities "Destination cities"
foreach x in MOZ TZA {
tsline   nnon_extraction_projects nnon_extraction_jobs nnb_subsectors nnb_cities  if time>-6 & time<6 &iso=="`x'", xlabel(-5(5)5) ylabel(0(200)700)  scheme(Plotplainblind)  xtitle(Discovery in year 0) xline(0) title(`x') legend(ring(0) position(11) bmargin(small)) xsize(5) ysize(5)  ylabel(,nogrid) xlabel(,nogrid)
graph save `x'boom_.gph,  replace
graph export "`x'boom_.pdf", as(pdf)  replace
}
restore
****************************************************
****************************************************

****************************************************
****************************************************
*FIGURE 6
*synthetic MOZ, ETH, GHA and TZA 
****************************************************
****************************************************

foreach x in MOZ ETH GHA TZA {
preserve
bys iso: egen max_d=max(discovery)
keep if iso=="`x'" | max_d!=.
gen hope="`x'"
drop if year==2015
keep hope iso year avg_project_size avg_non_extraction_size nb_source_countries nb_subsectors nb_sectors non_extraction_projects Total_FDI_projects non_extraction_FDI Total_FDI non_extraction_jobs Total_jobs
encode iso, gen(id)
tsset id year

if hope=="MOZ" {
synth    non_extraction_FDI non_extraction_FDI , trunit(63) trperiod(2009) xperiod(2003(1)2009) fig resultsperiod(2003(1)2014) keep(`x', replace) scheme(plotplainblind)  
graph save `x'temp.gph,  replace
}
if hope=="ETH" {
synth     non_extraction_FDI  non_extraction_FDI , trunit(27) trperiod(2010) xperiod(2003(1)2010) fig resultsperiod(2003(1)2014) keep(`x', replace) scheme(plotplainblind) 
graph save `x'temp.gph,  replace
}
if hope=="GHA" {
synth     non_extraction_FDI  non_extraction_FDI , trunit(34) trperiod(2007) xperiod(2003(1)2007) fig resultsperiod(2003(1)2014) keep(`x', replace) scheme(plotplainblind) 
graph save `x'temp.gph,  replace
}
if hope=="TZA" {
synth     non_extraction_FDI  non_extraction_FDI , trunit(113) trperiod(2010) xperiod(2003(1)2010) fig resultsperiod(2003(1)2014) keep(`x', replace) scheme(plotplainblind) 
graph save `x'temp.gph,  replace
}
restore
}
*decorative adjustments
foreach x in MOZ ETH GHA TZA {
graph use `x'temp.gph 
gr_edit .xaxis1.title.draw_view.setstyle, style(no)
gr_edit .legend.style.editstyle boxstyle(linestyle(color(none))) editcopy
gr_edit .legend.Edit, keepstyles
gr_edit .plotregion1.plot1.style.editstyle line(color(sea)) editcopy
gr_edit .plotregion1.plot2.style.editstyle line(color(turquoise)) editcopy
gr_edit .xaxis1.reset_rule 2003 2014 2 , tickset(major) ruletype(range) 
gr_edit .style.editstyle declared_xsize(4.5) editcopy
gr_edit .yaxis1.title.text = {}
gr_edit .yaxis1.title.text.Arrpush Non-extraction FDI
graph export `x'synth_.pdf, as(pdf)  replace
}
****************************************************
****************************************************

****************************************************
****************************************************
*FIGURE 5
*MOZ
****************************************************
****************************************************

preserve
keep if   year>2002 & year<2015
*tab GEO if iso=="MVH"|iso=="VPV"|iso=="DYG"|iso=="BQM"|iso=="WVF"
keep if GEO=="Africa"
*|iso=="MVH"|iso=="VPV"|iso=="DYG"|iso=="BQM"|iso=="WVF"
replace iso="Africa" if iso!="MOZ"
collapse  (sum) non_extraction_jobs non_extraction_projects , by(iso year)
reshape wide non_extraction_jobs non_extraction_projects, i(year) j(iso) string
gen share_jobs=non_extraction_jobsMOZ/ (non_extraction_jobsAfrica+non_extraction_jobsMOZ)
gen share_projects=non_extraction_projectsMOZ/ (non_extraction_projectsAfrica+non_extraction_projectsMOZ)

twoway (lpoly non_extraction_jobsMOZ year, bw(0.45)) (lpoly share_jobs year, yaxis(2)  bw(0.45)), scheme(Plotplainblind) xlabel(2002(2)2014) xtitle("")  ytitle("", axis(2)) ///
xline(2009) ylabel(0(0.025)0.125,axis(2))  ylabel(0(2000)12000,axis(1))  legend(pos(11)) ytitle("") legend(label(1 "Number of jobs (left axis)") label(2 "Share of sub-Saharan Africa")) xsize(4) ysize(4) title(Non-extraction FDI jobs)
graph export moz2_.pdf, as(pdf) replace

twoway (line non_extraction_projectsMOZ year) (line share_projects year, yaxis(2)), scheme(Plotplainblind) xlabel(2002(2)2014) xtitle("")  ytitle("", axis(2)) ///
xline(2009)  ylabel(0(0.025)0.125,axis(2))  ylabel(0(10)50,axis(1)) legend(pos(11)) ytitle("") legend(label(1 "Number of projects (left axis)") label(2 "Share of sub-Saharan Africa")) xsize(4) ysize(4) title(Non-extraction FDI projects)
graph export moz1_.pdf, as(pdf) replace
restore
****************************************************
****************************************************

*************************************
*************************************
*Regressions
*************************************
*************************************

****************************************************
****************************************************
*Preparing regressions
****************************************************
****************************************************

clear matrix
clear mata
clear
set maxvar 10000
use fdi_restat, clear
*the file with real country names --> fdi.dta 
encode iso, gen(id)
xtset id year


*deal with successive discoveries
gen disc_last_1yr=0 
replace disc_last_1yr=1 if discovery==1
replace disc_last_1yr=1 if l.discovery==1
replace disc_last_1yr=. if year==2015
lab var disc_last_1yr "Discovery last year"

*deal with successive discoveries
gen disc_last_2yr=0 
replace disc_last_2yr=1 if discovery==1
replace disc_last_2yr=1 if l.discovery==1
replace disc_last_2yr=1 if l2.discovery==1
replace disc_last_2yr=. if year==2015
lab var disc_last_2yr "Discovery in past 2 years"

*deal with successive discoveries
gen disc_last_3yr=0 
replace disc_last_3yr=1 if discovery==1
replace disc_last_3yr=1 if l.discovery==1
replace disc_last_3yr=1 if l2.discovery==1
replace disc_last_3yr=1 if l3.discovery==1
replace disc_last_3yr=. if year==2015
lab var disc_last_3yr "Discovery in past 3 years"

*deal with successive discoveries
gen disc_last_4yr=0 
replace disc_last_4yr=1 if discovery==1
replace disc_last_4yr=1 if l.discovery==1
replace disc_last_4yr=1 if l2.discovery==1
replace disc_last_4yr=1 if l3.discovery==1
replace disc_last_4yr=1 if l4.discovery==1
replace disc_last_4yr=. if year==2015
lab var disc_last_4yr "Discovery in past 4 years"


*deal with successive discoveries
gen disc_last_5yr=0 
replace disc_last_5yr=1 if discovery==1
replace disc_last_5yr=1 if l.discovery==1
replace disc_last_5yr=1 if l2.discovery==1
replace disc_last_5yr=1 if l3.discovery==1
replace disc_last_5yr=1 if l4.discovery==1
replace disc_last_5yr=1 if l5.discovery==1
replace disc_last_5yr=. if year==2015
lab var disc_last_5yr "Discovery in past 5 years"

*deal with pre-trend
gen disc_next_2yr=0 
replace disc_next_2yr=1 if f.discovery==1
replace disc_next_2yr=1 if f2.discovery==1
replace disc_next_2yr=. if year==2015
lab var disc_next_2yr "Discovery in next 2 years"

*pre-trend  dummies
gen disc_last_yr=0
gen disc_this_yr=0 
gen disc_next_yr=0 
gen disc_2_yr_ago=0 
replace disc_this_yr=1 if discovery==1
replace disc_last_yr=1 if l.discovery==1
replace disc_2_yr_ago=1 if l2.discovery==1
replace disc_next_yr=1 if f.discovery==1
replace disc_this_yr=. if year==2015
replace disc_last_yr=. if year==2015
replace disc_2_yr_ago=. if year==2015
replace disc_next_yr=. if year==2015
lab var disc_last_yr "Discovery last year"
lab var disc_2_yr_ago "Discovery 2 years ago"
lab var disc_next_yr "Discovery next year"
lab var disc_this_yr "Discovery this year"

*country-specific trends
sort id year
levelsof id, local(countries) 
foreach l of local countries {
by id: gen trend`l'=_n
replace trend`l'=0 if id!= `l'
}

*First discoveries
gen first_disc=0
replace first_disc=1 if discovery==1
forvalues i = 1(1)50 {
replace first_disc=0 if  l`i'.discovery ==1
}
lab var first_disc "First discovery"


*Preceding discoveries
bysort iso (year) : gen  precum=sum(discovery)
replace precum=precum-discovery
lab var precum "Nb of previous discoveries"

*identify only countries which get lucky in our sample period for conservative counterfactual
bys iso: egen max_d=max(discovery) if year>2002
replace max_d=0 if max_d!=1  & year >2002


xtset id year
*just first discoveries and 3 years after
gen drop=0
forvalues i = 4(1)50 {
replace drop=1 if  l`i'.discovery ==1
}

*nb of discoveries by country
bys iso: egen sum_d=sum(discovery) if year>2002
*replace sum_d=0 if sum_d==.  & year >2002

*we focus on positive fdi only from UNCTAD
*in absolut values this is less than 2%
replace fdi=0 if fdi<0

foreach x in  FDI_WDI non_extract_FDI_nex_countries non_extract_FDI_ex_countries avg_non_extraction_size avg_project_size ///
nb_source_countries nb_cities  nb_subsectors nb_sectors fdi size non_extraction_projects Total_FDI_projects non_extraction_FDI ///
 Total_FDI non_extraction_jobs Total_jobs{
gen ln`x'=ln(`x')
gen a`x'=asinh(`x')
}

*different measures of FDI
*foreach x in projects  JobsCreated  InvestmentUSm {
**biggest city versus the rest
*foreach y in 0 1 {
*Industries
*foreach z in Manufacturing Services {
*gen a`x'`y'`z'=asinh(`x'`y'`z')
*}
*}
*}

*we focus on the predetminerd "sectors" by fdiMarkets in the variable BusinessActivity
foreach x in BusinessServices Construction CustomerContactCentre DesignDevelopmentTesting EducationTraining Electricity Extraction Headquarters /// 
ICTInternetInfrastructure Logistics MaintenanceServicing Manufacturing Recycling ResearchDevelopment ///
 Retail SalesMarketingSupport SharedServicesCentre TechnicalSupportCentre {
gen  aF`x'=asinh(F`x')
lab var aF`x' "`x'"
gen aP`x'=asinh(P`x')
lab var aP`x' "`x'"
}

foreach x in ln a {
lab var `x'avg_project_size "Avg project size"
lab var `x'avg_non_extraction_size "Avg non-extraction project size"
lab var `x'Total_jobs "Total jobs created"
lab var `x'non_extraction_jobs "Jobs created - non-extraction"
lab var `x'Total_FDI "Total FDI (USD million)"
lab var `x'non_extraction_FDI "Non-extraction FDI (USD million)"
lab var `x'Total_FDI_projects "Nb FDI projects"
lab var `x'non_extraction_projects "Nb non-extraction FDI projects"
lab var `x'size "Discovery discounted net value (\% GDP)"
lab var `x'fdi "Current USD FDI (UNCTAD)"
lab var `x'nb_source_countries "Nb source countries"
lab var `x'nb_subsectors "Nb sub-sectors"
lab var `x'nb_sectors  "Nb sectors"
lab var `x'nb_cities  "Nb cities"
lab var `x'non_extract_FDI_ex_countries "From extracting countries"
lab var `x'non_extract_FDI_nex_countries "From other countries"
}

lab var fdi "Current USD FDI (UNCTAD)"
lab var FEU "FDI from EU"
lab var FOECD "FDI from OECD"
lab var FnonOECD  "FDI from non-OECD"
lab var Favoidrisk "FDI from risk-avoiding countries"
lab var Floverisk  "FDI from risk-loving countries"

foreach x in a {
lab var `x'avg_non_extraction_size "Avg project size"
lab var `x'non_extraction_jobs "Jobs created"
lab var `x'non_extraction_FDI "FDI (USD million)"
lab var `x'non_extraction_projects "Nb projects"
}

lab var avg_non_extraction_size "Avg project size"
lab var non_extraction_jobs "Jobs created"
lab var non_extraction_FDI "FDI (USD million)"
lab var non_extraction_projects "Nb projects"

*just one observations is negative
replace non_extraction_FDI=0  if non_extraction_FDI <0

*only exploration rather than discovery countries
*identify only drilling countries for alternative counterfactual
*data for this counterfactual is coming from the same data 
*as used to create the maps above. 
*bys iso: egen max_wells=max(wells)
bys iso: egen exploration=sum(wells) if year>2002
gen max_e=0
replace max_e=1 if exploration>0
 
 
****************************************************
****************************************************
*TABLE A1
*sum stats table
****************************************************
****************************************************

fsum Total_FDI non_extraction_FDI    Total_FDI_projects non_extraction_projects  ///
Total_jobs non_extraction_jobs   avg_project_size avg_non_extraction_size ///
nb_source_countries nb_subsectors nb_sectors nb_cities     ///
fdi   disc_last_2yr   if disc_last_2yr!=. & aTotal_FDI_projects!=. & max_d!=2 ,  stat( mean sd min max )   uselabel

****************************************************
****************************************************

****************************************************
****************************************************
*FIGURE A4
*comparing UNCTAD and fDi Intelligence
****************************************************
****************************************************

twoway (scatter afdi aTotal_FDI) (line  aTotal_FDI aTotal_FDI)  if year<2015 & year>2002, title(FDI inflows) scheme(Plotplainblind) legend(off) ytitle(UNCTAD) xtitle(fDiMarkets) xsize(4) ysize(4) 
graph export fdidata1_.pdf, as(pdf)  replace

twoway (kdensity afdi) (kdensity aTotal_FDI)  if year<2015 & year>2002, scheme(Plotplainblind) xtitle("") title(FDI inflows) ytitle(pdf) legend(position (6)) xsize(4) ysize(4) legend(label(1 "UNCTAD") label(2 "fDiMarkets")) 
graph export fdidata2_.pdf, as(pdf)  replace

****************************************************
****************************************************

****************************************************
****************************************************
*TABLE 1
****************************************************
****************************************************

eststo clear
foreach z in a {
foreach x in   non_extraction_FDI  non_extraction_projects avg_non_extraction_size  non_extraction_jobs  {
eststo: reghdfe `z'`x' disc_last_2yr if max_e==1 , absorb(id year) cluster(id year) nocons
}
}
esttab using non_extractive3.tex, replace label nogaps se b(%9.3f) star(* 0.10 ** 0.05 *** 0.01) stats(N r2, fmt(%9.0g %9.2f %9.2f %9.2f %9.2f) labels(N "R-sq")) nonote drop()


eststo clear
foreach z in a {
foreach x in   nb_source_countries nb_subsectors nb_sectors nb_cities  {
eststo: reghdfe `z'`x' disc_last_2yr if max_e==1, absorb(id year) cluster(id year) nocons
}
}
esttab using extensive3.tex, replace label nogaps se b(%9.3f) star(* 0.10 ** 0.05 *** 0.01) stats(N r2, fmt(%9.0g %9.2f %9.2f %9.2f %9.2f) labels(N "R-sq")) nonote drop()
****************************************************
****************************************************

****************************************************
****************************************************
*TABLES A.2 and A.3
****************************************************
****************************************************

foreach y in 2 0 {
eststo clear
foreach z in a {
foreach x in  non_extraction_FDI  non_extraction_projects avg_non_extraction_size  non_extraction_jobs {
eststo: reghdfe `z'`x' disc_last_2yr if max_d!=`y', absorb(id year) cluster(id year) nocons
}
}
esttab using non_extractive`y'.tex, replace label nogaps se b(%9.3f) star(* 0.10 ** 0.05 *** 0.01) stats(N r2, fmt(%9.0g %9.2f %9.2f %9.2f %9.2f) labels(N "R-sq")) nonote drop()

eststo clear
foreach z in a {
foreach x in   nb_source_countries nb_subsectors nb_sectors nb_cities  {
eststo: reghdfe `z'`x' disc_last_2yr if max_d!=`y', absorb(id year) cluster(id year) nocons
}
}
esttab using extensive`y'.tex, replace label nogaps se b(%9.3f) star(* 0.10 ** 0.05 *** 0.01) stats(N r2, fmt(%9.0g %9.2f %9.2f %9.2f %9.2f) labels(N "R-sq")) nonote drop()
}
****************************************************
****************************************************

****************************************************
****************************************************
*TABLE A.4
*UNCTAD Table
****************************************************
****************************************************

eststo clear
eststo: reghdfe afdi disc_last_2yr if max_d!=2, absorb(id year) cluster(id year)
eststo: reghdfe afdi disc_last_2yr if max_e!=0, absorb(id year) cluster(id year)
eststo: reghdfe afdi disc_last_2yr if max_d!=0, absorb(id year) cluster(id year)
esttab using unctad1.tex, replace label nogaps se b(%9.3f) star(* 0.10 ** 0.05 *** 0.01) stats(N r2, fmt(%9.0g %9.2f %9.2f %9.2f %9.2f) labels(N "R-sq")) nonote

eststo clear
eststo: reghdfe afdi disc_last_2yr if max_d!=2  & non_extraction_FDI!=., absorb(id year) cluster(id year)
eststo: reghdfe afdi disc_last_2yr if max_e!=0  & non_extraction_FDI!=., absorb(id year) cluster(id year)
eststo: reghdfe afdi disc_last_2yr if max_d!=0  & non_extraction_FDI!=., absorb(id year) cluster(id year)
esttab using unctad2.tex, replace label nogaps se b(%9.3f) star(* 0.10 ** 0.05 *** 0.01) stats(N r2, fmt(%9.0g %9.2f %9.2f %9.2f %9.2f) labels(N "R-sq")) nonote

****************************************************
****************************************************

****************************************************
****************************************************
*FIGURE A.3
***different time horizons
****************************************************
****************************************************

lab var discovery "Discovery year"
foreach y in 1 2 3 4 5{
lab var disc_last_`y'yr "Discovery year + `y'" 
}

foreach b in non_extraction_FDI non_extraction_projects avg_non_extraction_size  non_extraction_jobs {
eststo clear
eststo: reghdfe a`b' discovery if max_e==1, absorb(id year) cluster(id year) nocons
est sto `b'0
foreach y in 1 2 3 4 5{
eststo: reghdfe a`b' disc_last_`y'yr  if max_e==1, absorb(id year) cluster(id year) nocons
*esttab using horizons`b'.tex, replace label nogaps se b(%9.3f) star(* 0.10 ** 0.05 *** 0.01) stats(N r2, fmt(%9.0g %9.2f %9.2f %9.2f %9.2f) labels(N "R-sq")) nonote drop()
est sto `b'`y'
}
}

coefplot non_extraction_FDI0 non_extraction_FDI1 non_extraction_FDI2 non_extraction_FDI3 non_extraction_FDI4 non_extraction_FDI5  ///
||  non_extraction_projects0 non_extraction_projects1 non_extraction_projects2 non_extraction_projects3 non_extraction_projects4 non_extraction_projects5   ///
|| avg_non_extraction_size0  avg_non_extraction_size1  avg_non_extraction_size2  avg_non_extraction_size3  avg_non_extraction_size4  avg_non_extraction_size5 ///
||  non_extraction_jobs0 non_extraction_jobs1 non_extraction_jobs2 non_extraction_jobs3 non_extraction_jobs4 non_extraction_jobs5 ///
, legend(off)   levels(90) xline(0) xsize(6.6) ysize(3)   ciopts(recast(rcap)) scheme(s1mono) 

gr_edit .style.editstyle declared_xsize(5) editcopy
gr_edit .style.editstyle aspect_pos(north) editcopy
gr_edit .Edit , cmd(.set_cols = 4) cmd(.set_rows = 0)
gr_edit .legend.draw_view.setstyle, style(no)
gr_edit .plotregion1.subtitle[1].text = {}
gr_edit .plotregion1.subtitle[1].text.Arrpush FDI (USD million)
gr_edit .plotregion1.subtitle[2].text = {}
gr_edit .plotregion1.subtitle[2].text.Arrpush FDI projects
gr_edit .plotregion1.subtitle[3].text = {}
gr_edit .plotregion1.subtitle[3].text.Arrpush Avg project size
gr_edit .plotregion1.subtitle[4].text = {}
gr_edit .plotregion1.subtitle[4].text.Arrpush Jobs created
graph export horizons_.pdf, as(pdf) replace

****************************************************
****************************************************
****************************************************
****************************************************
*FIGURE 3
***Leads and lags
*just first discoveries and 10 years before/after
****************************************************
****************************************************

xtset id year
gen f1first_disc=1

reghdfe anon_extraction_FDI l(-6/-2).first_disc  f1first_disc l(0/6).first_disc  if  max_d==1 | max_d==0 , absorb(id year) cluster(id year) nocons
coefplot,     ciopts(  recast(rarea) ) vertical   levels(90) scheme(plotplainblind)  play(leads_lags) ylabel(-4(1)5, nogrid) yline(0, lpattern(solid) lcolor(gray))
graph play UNCTAD
graph play correct
graph export leads_lags_.pdf, as(pdf)  replace

*with UNCTAD data
reghdfe afdi l(-6/-2).first_disc  f1first_disc l(0/6).first_disc   if  max_d==1 | max_d==0 ,  absorb(id year) cluster(id year) nocons
coefplot,       ciopts(  recast(rarea) ) vertical   levels(90) scheme(plotplainblind) play(leads_lags) ylabel(-4(1)5, nogrid) yline(0, lpattern(solid) lcolor(gray))
graph play UNCTAD
graph export leads_lags_UNCTAD_.pdf, as(pdf)  replace

***cumulative graph

reghdfe anon_extraction_FDI l(-6/-2).first_disc  f1first_disc l(0/6).first_disc      if  max_d==1 | max_d==0 , absorb(id year) cluster(id year) nocons
estimates store  coef

estimates restore  coef
lincomest  f6.first_disc, level(95)
parmest,label level(90 95) saving(cumtreat2,replace)

estimates restore  coef
lincomest  f6.first_disc+f5.first_disc  , level(95)
parmest,label level(90 95) saving(cumtreat3,replace)

estimates restore  coef
lincomest f6.first_disc+f5.first_disc + f4.first_disc, level(95)
parmest,label level(90 95) saving(cumtreat4,replace)

estimates restore  coef
lincomest  f6.first_disc+f5.first_disc + f4.first_disc+ f3.first_disc , level(95)
parmest,label level(90 95) saving(cumtreat5,replace)

estimates restore  coef
lincomest   f6.first_disc+f5.first_disc + f4.first_disc+ f3.first_disc+ f2.first_disc , level(95)
parmest,label level(90 95) saving(cumtreat6,replace)

estimates restore  coef
lincomest f1first_disc , level(95)
parmest,label level(90 95) saving(cumtreat7,replace)

estimates restore  coef
lincomest   first_disc , level(95)
parmest,label level(90 95) saving(cumtreat8,replace)

estimates restore  coef
lincomest   first_disc+ l1.first_disc, level(95)
parmest,label level(90 95) saving(cumtreat9,replace)


estimates restore  coef
lincomest   first_disc+ l1.first_disc+ l2.first_disc, level(95)
parmest,label level(90 95) saving(cumtreat10,replace)


estimates restore  coef
lincomest   first_disc+ l1.first_disc+ l2.first_disc+ l3.first_disc, level(95)
parmest,label level(90 95) saving(cumtreat11,replace)


estimates restore  coef
lincomest   first_disc+ l1.first_disc+ l2.first_disc+ l3.first_disc+ l4.first_disc, level(95)
parmest,label level(90 95) saving(cumtreat12,replace)

estimates restore  coef
lincomest   first_disc+ l1.first_disc+ l2.first_disc+ l3.first_disc+ l4.first_disc+ l5.first_disc, level(95)
parmest,label level(90 95) saving(cumtreat13,replace)

estimates restore  coef
lincomest   first_disc+ l1.first_disc+ l2.first_disc+ l3.first_disc+ l4.first_disc+ l5.first_disc+ l6.first_disc, level(95)
parmest,label level(90 95) saving(cumtreat14,replace)

preserve
use "cumtreat2.dta", clear 
local i=3
while `i'<=14{
append using "cumtreat`i'.dta"
local i=`i'+1
}

gen tt=_n

twoway (line estimate tt,  yline(0, lpattern(solid) lcolor(gray)) xline(6, lpattern(solid) lcolor(gray)) legend(off) ylabel(, angle(horizontal))  ///
lwidth(0.1) graphregion(color(white))  plotregion(fcolor(gs16))  lcolor(blue) lw(thick) title("Cumualtive FDI inflow") ylabel(-10(5)20, nogrid) /// 
xlabel(1 "-6" 2 "-5" 3 "-4" 4 "-3" 5 "-2" 6 "-1" 7 "0" 8 "+1" 9 "+2" 10 "+3" 11 "+4" 12 "+5" 13 "+6"  ) ///
ytitle("Estimate of {&Beta}{subscript:l}") xtitle("Number of lag l") )  ///
(line min95 tt, lwidth(0.1) lpattern(dot) col(black) lw(thick) ) (line max95 tt, lwidth(0.1) col(black) lpattern(dot) lw(thick))  ///
(line min90 tt, lwidth(0.1) lpattern(dash) col(black) lw(medthick) ) (line max90 tt, lwidth(0.1) col(black) lpattern(dash) lw(medthick))  
graph play gees
graph export leads_lags_cum_.pdf, as(pdf)  replace

restore

***cumulative graph UNCTAD data
reghdfe afdi l(-6/-2).first_disc  f1first_disc l(0/6).first_disc    if  max_d==1 | max_d==0 , absorb(id year) cluster(id year) nocons
estimates store  coef

estimates restore  coef
lincomest  f6.first_disc, level(95)
parmest,label level(90 95) saving(cumtreat2,replace)

estimates restore  coef
lincomest  f6.first_disc+f5.first_disc  , level(95)
parmest,label level(90 95) saving(cumtreat3,replace)

estimates restore  coef
lincomest f6.first_disc+f5.first_disc + f4.first_disc, level(95)
parmest,label level(90 95) saving(cumtreat4,replace)

estimates restore  coef
lincomest  f6.first_disc+f5.first_disc + f4.first_disc+ f3.first_disc , level(95)
parmest,label level(90 95) saving(cumtreat5,replace)

estimates restore  coef
lincomest   f6.first_disc+f5.first_disc + f4.first_disc+ f3.first_disc+ f2.first_disc , level(95)
parmest,label level(90 95) saving(cumtreat6,replace)

estimates restore  coef
lincomest f1first_disc , level(95)
parmest,label level(90 95) saving(cumtreat7,replace)

estimates restore  coef
lincomest   first_disc , level(95)
parmest,label level(90 95) saving(cumtreat8,replace)

estimates restore  coef
lincomest   first_disc+ l1.first_disc, level(95)
parmest,label level(90 95) saving(cumtreat9,replace)


estimates restore  coef
lincomest   first_disc+ l1.first_disc+ l2.first_disc, level(95)
parmest,label level(90 95) saving(cumtreat10,replace)


estimates restore  coef
lincomest   first_disc+ l1.first_disc+ l2.first_disc+ l3.first_disc, level(95)
parmest,label level(90 95) saving(cumtreat11,replace)


estimates restore  coef
lincomest   first_disc+ l1.first_disc+ l2.first_disc+ l3.first_disc+ l4.first_disc, level(95)
parmest,label level(90 95) saving(cumtreat12,replace)

estimates restore  coef
lincomest   first_disc+ l1.first_disc+ l2.first_disc+ l3.first_disc+ l4.first_disc+ l5.first_disc, level(95)
parmest,label level(90 95) saving(cumtreat13,replace)

estimates restore  coef
lincomest   first_disc+ l1.first_disc+ l2.first_disc+ l3.first_disc+ l4.first_disc+ l5.first_disc+ l6.first_disc, level(95)
parmest,label level(90 95) saving(cumtreat14,replace)

preserve
use "cumtreat2.dta", clear 
local i=3
while `i'<=14{
append using "cumtreat`i'.dta"
local i=`i'+1
}

gen tt=_n

twoway (line estimate tt,  yline(0, lpattern(solid) lcolor(gray)) xline(6, lpattern(solid) lcolor(gray)) legend(off) ylabel(, angle(horizontal))  ///
lwidth(0.1) graphregion(color(white))  plotregion(fcolor(gs16))  lcolor(blue) lw(thick) title("Cumualtive FDI inflow") ylabel(-10(5)20, nogrid) /// 
xlabel(1 "-6" 2 "-5" 3 "-4" 4 "-3" 5 "-2" 6 "-1" 7 "0" 8 "+1" 9 "+2" 10 "+3" 11 "+4" 12 "+5" 13 "+6"  ) ///
ytitle("Estimate of {&Beta}{subscript:l}") xtitle("Number of lag l") )  ///
(line min95 tt, lwidth(0.1) lpattern(dot) col(black) lw(thick) ) (line max95 tt, lwidth(0.1) col(black) lpattern(dot) lw(thick))  ///
(line min90 tt, lwidth(0.1) lpattern(dash) col(black) lw(medthick) ) (line max90 tt, lwidth(0.1) col(black) lpattern(dash) lw(medthick))  
graph play gees
graph play UNCTAD2
graph export leads_lags_UNCTAD_cum_.pdf, as(pdf)  replace
restore
****************************************************
****************************************************

****************************************************
****************************************************
*FIGURE A.1  (inlcuded as a pdf with country names)
****************************************************
****************************************************

preserve
keep if  max_d==1 & year>2002 & year<2015
collapse  non_extraction_projects nb_sectors nb_subsectors nb_source_countries, by(iso)
drop if non_extraction_projects==.
gsort  -non_extraction_projects
gen trend=_n
replace  non_extraction_projects=415 if non_extraction_projects>700 & non_extraction_projects<1000
replace  non_extraction_projects=450 if non_extraction_projects>1000
graph hbar  non_extraction_projects  , over(iso, sort(trend)) bar(1,color(sea)) xsize(4) ysize(8) ylabel(0(100)500) ytitle(Non-extraction projects)
graph save temp1, replace
graph hbar  nb_sectors  nb_source_countries  , over(iso, sort(trend)) bar(1,color(sea)) bar(2,color(lime)) xsize(4) ysize(8)  legend(label(1 "Sectors") label(2 "Source countries")) 
graph save temp2, replace
graph combine temp1.gph temp2.gph, xsize(7) ysize(4)
gr_edit  .plotregion1.graph1.plotregion1.AddTextBox added_text editor 98.51033883584509 461.1558278550777
gr_edit  .plotregion1.graph1.plotregion1.added_text_new = 1
gr_edit  .plotregion1.graph1.plotregion1.added_text_rec = 1
gr_edit  .plotregion1.graph1.plotregion1.added_text[1].style.editstyle  angle(default) size(medsmall) color(cranberry) horizontal(left) vertical(middle) margin(zero) linegap(zero) drawbox(no) boxmargin(zero) fillcolor(bluishgray) linestyle( width(thin) color(black) pattern(solid)) box_alignment(east) editcopy
gr_edit  .plotregion1.graph1.plotregion1.added_text[1].text = {}
gr_edit  .plotregion1.graph1.plotregion1.added_text[1].text.Arrpush 1335
gr_edit  .plotregion1.graph1.plotregion1.added_text[1].style.editstyle color(black) editcopy
gr_edit  .plotregion1.graph1.plotregion1.added_text[1].DragBy .0678110601850389 -6.405023976625246
gr_edit  .plotregion1.graph1.plotregion1.added_text[1].DragBy 0 -2.260596697632512
gr_edit  .plotregion1.graph1.plotregion1.AddTextBox added_text editor 94.23824204418526 425.7398129255033
gr_edit  .plotregion1.graph1.plotregion1.added_text_new = 2
gr_edit  .plotregion1.graph1.plotregion1.added_text_rec = 2
gr_edit  .plotregion1.graph1.plotregion1.added_text[2].style.editstyle  angle(default) size(medsmall) color(cranberry) horizontal(left) vertical(middle) margin(zero) linegap(zero) drawbox(no) boxmargin(zero) fillcolor(bluishgray) linestyle( width(thin) color(black) pattern(solid)) box_alignment(east) editcopy
gr_edit  .plotregion1.graph1.plotregion1.added_text[2].text = {}
gr_edit  .plotregion1.graph1.plotregion1.added_text[2].text.Arrpush 752
gr_edit  .plotregion1.graph1.plotregion1.added_text[2].style.editstyle color(black) editcopy
gr_edit  .plotregion1.graph1.plotregion1.added_text[2].DragBy -.2034331805552064 -6.405023976625174
graph export figureA1_.pdf, as(pdf) replace
restore
****************************************************
****************************************************

****************************************************
****************************************************
*FIGURE A.5
*non-linear grinters
****************************************************
****************************************************

*GDP per captia and discoveries
preserve
gen lngdppc=ln(GDPPC_real)
gen inte=lngdppc*disc_last_2yr
foreach x in  anon_extraction_FDI  {
reghdfe `x'  inte disc_last_2yr , absorb(id year) cluster(id year) 
matrix b=e(b)
matrix V=e(V)
scalar b1=b[1,1]
scalar b2=b[1,2]
scalar varb1=V[1,1]
scalar varb2=V[2,2]
scalar covb1b2=V[1,2]
gen beta`x'=b2 + lngdppc* b1
gen se`x'=sqrt(varb2+ varb1*(lngdppc^2) + 2*(lngdppc)*covb1b2)
gen upper`x'=beta`x'+1.96*se`x'
gen lower`x'=beta`x'-1.96*se`x'
}

collapse (mean)  beta* upper* lower* lngdppc, by(iso)
gen punk=exp( lngdppc)
graph twoway (fpfit beta`x' punk)  (fpfit lower`x' punk, lpattern(dash))   (fpfit upper`x' punk, lpattern(dash)) (kdensity punk, yaxis(2)) if punk<10000, scheme(plotplainblind)  legend(off) play(grinter1)
graph export "grinter_gdppc_.pdf", as(pdf)  replace
restore

*IQ and discoveries
preserve
bysort iso: egen reg_qua=mean(regulatory_quality)
bysort iso: egen law=mean(rule_of_law)
gen inte=governance*disc_last_2yr
foreach x in  anon_extraction_FDI  {
reghdfe `x'  inte disc_last_2yr , absorb(id year) cluster(id year) 
matrix b=e(b)
matrix V=e(V)
scalar b1=b[1,1]
scalar b2=b[1,2]
scalar varb1=V[1,1]
scalar varb2=V[2,2]
scalar covb1b2=V[1,2]
gen beta`x'=b2 + governance* b1
gen se`x'=sqrt(varb2+ varb1*(governance^2) + 2*(governance)*covb1b2)
gen upper`x'=beta`x'+1.96*se`x'
gen lower`x'=beta`x'-1.96*se`x'
}
*
collapse (mean)  beta* upper* lower* gov law reg_qua, by(iso)
gen punk=governance
graph twoway (fpfit beta`x' punk)  (fpfit lower`x' punk, lpattern(dash))   (fpfit upper`x' punk, lpattern(dash)) (kdensity punk, yaxis(2)) , scheme(plotplainblind)  legend(off) play(grinter1)
gr_edit .xaxis1.title.text = {}
gr_edit .xaxis1.title.text.Arrpush CPIA property rights and rule-based governance rating (1=low to 6=high)
*gr_edit .xaxis1.title.text.Arrpush Regulatory quality
*gr_edit .xaxis1.title.text.Arrpush Rule of law
graph export "grinter_gov_.pdf", as(pdf)  replace
restore

*Interacting with previous dicoveries
preserve
gen inte=disc_last_2yr*precum
gen temp=precum

foreach x in  anon_extraction_FDI  {
reghdfe `x'  inte disc_last_2yr temp , absorb(id year) cluster(id year) 
matrix b=e(b)
matrix V=e(V)
scalar b1=b[1,1]
scalar b2=b[1,2]
scalar varb1=V[1,1]
scalar varb2=V[2,2]
scalar covb1b2=V[1,2]
gen beta`x'=b2 + temp* b1
gen se`x'=sqrt(varb2+ varb1*(temp^2) + 2*(temp)*covb1b2)
gen upper`x'=beta`x'+1.645*se`x'
gen lower`x'=beta`x'-1.645*se`x'
}
*
collapse (mean)  beta* upper* lower* temp , by(iso)
gen punk=temp
graph twoway  (fpfit beta`x' punk)  (fpfit lower`x' punk, lpattern(dash))   (fpfit upper`x' punk, lpattern(dash))  (kdensity punk, yaxis(2)), scheme(plotplainblind)  legend(off) play(grinter1)
*graph edit chnage density line to spike
gr_edit .xaxis1.title.text = {}
gr_edit .xaxis1.title.text.Arrpush Nb of previous discoveries
graph export "grinter_cum_.pdf", as(pdf)  replace
restore

*FDI GRAVITY*
preserve
use fdi_gravity_restat, replace
gen dist_disc=distance*disc_last_2yr
reghdfe aTotal_FDI    dist_disc disc_last_2yr   , absorb(o_year pair) cluster(iso_d)
matrix b=e(b)
matrix V=e(V)
scalar b1=b[1,2]
scalar b2=b[1,1]
scalar varb1=V[2,2]
scalar varb2=V[1,1]
scalar covb1b2=V[2,1]
gen beta=b1 + distance* b2
gen se=sqrt(varb1+ varb2*(distance^2) + 2*(distance)*covb1b2)
gen upper=beta+1.96*se
gen lower=beta-1.96*se
*
collapse (mean)  beta* upper* lower* , by(distance)
gen punk=exp(distance)
graph twoway (fpfit beta punk)  (fpfit lower punk, lpattern(dash) lcolor(sky))   (fpfit upper punk, lpattern(dash) lcolor(sky)) (kdensity punk, yaxis(2)   ), scheme(plotplainblind) xsca(log) xlab(1 10 100 1000 5000 20000, grid) ///
 ylab(,grid) ytitle(Discovery effect on non-extraction FDI inflows) ytitle("",axis(2)) ylab("",axis(2)) xtitle(Distance between FDI source and discovery countries (km)) legend(off)
graph export "grinter_dist_.pdf", as(pdf)  replace
restore
****************************************************
****************************************************

****************************************************
****************************************************
*FIGURE 4
*by FDI sector, just main city
****************************************************
****************************************************

preserve
keep if year<2015 & year>2002
foreach x in BusinessServices Construction CustomerContactCentre DesignDevelopmentTesting EducationTraining Electricity Extraction Headquarters /// 
ICTInternetInfrastructure Logistics MaintenanceServicing Manufacturing Recycling ResearchDevelopment Retail SalesMarketingSupport SharedServicesCentre TechnicalSupportCentre {
gen aFC`x'=asinh(FC`x')
lab var aFC`x' "`x'"
eststo: reghdfe aFC`x' disc_last_2yr if max_d!=2 , absorb(id year) cluster(id year)
matrix b=e(b)
matrix V=e(V)
scalar b=b[1,1]
scalar varb=V[1,1]
gen b`x'=b
gen s`x'=sqrt(varb)
gen u`x'=b`x'+1.645*s`x'
gen l`x'=b`x'-1.645*s`x'
}
*
keep b* u* l*
drop ln*
*drop beta* upper* lower*
gen trend=_n
keep if trend==1
reshape long b l u, i(trend) j(sector) string
drop if b==0
gen zero=-3
sort b
replace trend=_n
eclplot b l u trend  , horizontal   plot(scatter  trend zero , msymbol(none) mlabel( sector ) mlabsize(medium)) scheme(plotplainblind) ytitle("") ylabel(none)   xtitle("Discovery effect") xsize(4.2) ysize(3.7) xline(0)  
graph export "activity_city_.pdf", as(pdf)  replace
restore

preserve
keep if year<2015 & year>2002
foreach x in BusinessServices Construction CustomerContactCentre DesignDevelopmentTesting EducationTraining Electricity Extraction Headquarters /// 
ICTInternetInfrastructure Logistics MaintenanceServicing Manufacturing Recycling ResearchDevelopment Retail SalesMarketingSupport SharedServicesCentre TechnicalSupportCentre {
gen aFR`x'=asinh(F`x'-FC`x')
lab var aFR`x' "`x'"
eststo: reghdfe aFR`x' disc_last_2yr if max_d!=2 , absorb(id year) cluster(id)
matrix b=e(b)
matrix V=e(V)
scalar b=b[1,1]
scalar varb=V[1,1]
gen b`x'=b
gen s`x'=sqrt(varb)
gen u`x'=b`x'+1.645*s`x'
gen l`x'=b`x'-1.645*s`x'
}
*
keep b* u* l*
drop ln*
*drop beta* upper* lower*
gen trend=_n
keep if trend==1
reshape long b l u, i(trend) j(sector) string
drop if b==0
gen zero=-3
sort b
replace trend=_n
eclplot b l u trend  , horizontal   plot(scatter  trend zero , msymbol(none) mlabel( sector ) mlabsize(medium)) scheme(plotplainblind) ytitle("") ylabel(none)   xtitle("Discovery effect") xsize(4.2) ysize(3.7) xline(0) 
graph export "activity_other_.pdf", as(pdf)  replace
restore

****************************************************
****************************************************
*FIGURE A2
*placebo discoveries within discovery countries
****************************************************
****************************************************

preserve 
drop if year<2003&year<2015
xtset id year
sort id year
set seed 1
forvalues i = 1(1)500 {
xtset id year
sort id year
shufflevar disc_last_2yr, cluster(id)
gen placebo`i'=disc_last_2yr_shuffled
drop disc_last_2yr_shuffled
xtset id year
reghdfe   anon_extraction_FDI placebo`i' if max_e==1, absorb(id year) cluster(id year)
matrix b=e(b)
matrix V=e(V)
scalar b=b[1,1]
scalar varb=V[1,1]
gen beta`i'=b
gen se`i'=sqrt(varb)
gen upper`i'=beta`i'+1.645*se`i'
gen lower`i'=beta`i'-1.645*se`i'
}
keep beta* upper* lower*
gen trend=_n
keep if trend==1
reshape long beta lower upper, i(trend) j(emp) 
replace trend=_n
*eclplot beta lower upper trend , horizontal  scheme(plotplainblind) ytitle("") ylabel(none)   xtitle("Placebo effect") xsize(4.2) ysize(3.7) xline(0) title(Placebo discoveries) legend(label(1 "500 placebo discoveries") lab(2 "Estimated discovery effect"))
twoway (kdensity beta )  , title("") xtitle(Estimated placebo effects) text( 2 0.72 "Baseline")  text( 1.9 0.72 "Estimate") ytitle(Density) xline(.594) xsize(6) ysize(4) xlabel(-1(.5)1,  nogrid) ylabel(,  nogrid) scheme(plotplainblind) 
graph export placeboss_.pdf, as(pdf)  replace
restore

****************************************************
****************************************************



*******************************************************************************
*******************************************************************************
*******************************************************************************
*******************************************************************************

*********** MOZAMBIQUE PART ***************

*******************************************************************************
*******************************************************************************
*******************************************************************************
*******************************************************************************


****************************************************
****************************************************
*HH, FIRMS and FDI
****************************************************
****************************************************


use clean_hh_firms_FDI_ano.dta,replace
*the file with real district names --> clean_hh_firms_FDI.dta 

*we can not use this year since we cannot link it to the firm level census
drop if year==2008

xtset district_sector period_two

*Assuming no fdi in 2002
gen pessoal_adjFDI2014=pessoal if year==2002
replace pessoal_adjFDI2014=pessoal-FDI_stock_jobs if year==2014
gen FDI_stock_jobs_adj2002=FDI_stock_jobs
replace FDI_stock_jobs_adj2002=0 if year==2002
gen firms_adjFDI2014=total_firms if year==2002
replace firms_adjFDI2014=total_firms-FDI_stock if year==2014
gen FDI_stock_adj2002=FDI_stock
replace FDI_stock_adj2002=0 if year==2002

*adjust for calcualted FDI
gen pessoal_adjFDI=pessoal-FDI_stock_jobs
gen firms_adjFDI=total_firms-FDI_stock

*adjust for fdi from ft
gen firms_adjFDIft=total_firms if year==2002
replace firms_adjFDIft=total_firms-cum_projects if year==2014
gen pessoal_adjFDIft=pessoal if year==2002
replace pessoal_adjFDIft=pessoal-cum_jobs if year==2014

*no such thing as negative employment or negative nb of firms: replace with zero
foreach x in people pessoal_adjFDI2014  FDI_stock_jobs FDI_stock_jobs_adj2002 cum_jobs pessoal_adjFDIft pessoal_adjFDI firms_adjFDI   firms_adjFDI2014 firms_adjFDIft FDI_stock  FDI_stock_adj2002  cum_projects{
replace `x'=0 if `x'<0
}


label var  people "Individuals in HH"

label var  pessoal_adjFDI "Local employees (account FDI)"
label var  pessoal_adjFDI2014 "Local employees (account FDI2014)"
label var  pessoal_adjFDIft "Local employees (account FDI FT)"

label var  firms_adjFDI "Local projects (account FDI)"
label var  firms_adjFDI2014 "Local projects (account FDI2014)"
label var  firms_adjFDIft "Local projects (account FDI FT)"

label var  FDI_stock_jobs "FDI jobs Census"
label var  FDI_stock_jobs_adj2002 "FDI jobs Census (no FDI in 2002)"
label var  cum_jobs "FDI jobs FT"

label var  FDI_stock "FDI Census"
label var  FDI_stock_adj2002 "FDI Census (no FDI in 2002)"
label var  cum_projects "FDI FT"

gen total_jobs=people if alternativesector!=-1 

gen non_FDI_jobs1=people-FDI_stock_jobs
gen non_FDI_jobs2=people-FDI_stock_jobs_adj2002
gen non_FDI_jobs3=people-cum_jobs

gen informal_jobs1=people-FDI_stock_jobs-pessoal_adjFDI
gen informal_jobs2=people-FDI_stock_jobs_adj2002-pessoal_adjFDI2014
gen informal_jobs3=people-cum_jobs-pessoal_adjFDIft

gen formal_jobs1=pessoal_adjFDI
gen formal_jobs2=pessoal_adjFDI2014
gen formal_jobs3=pessoal_adjFDIft

foreach x in  total_jobs non_FDI_jobs1 non_FDI_jobs2 non_FDI_jobs3 informal_jobs1 informal_jobs2 informal_jobs3 formal_jobs1 formal_jobs2 formal_jobs3{
replace `x'=0 if `x'<0
replace `x'=. if alternativesector==-1
}

****************************************************
****************************************************
*TABLE A.7
****************************************************
****************************************************

tabstat total_jobs  informal_jobs1 formal_jobs1 FDI_stock_jobs educated noteducated women men if year==2014 & sectorname!="Unemployment", by(sectorname) stat(sum) format(%9.3g)

****************************************************
****************************************************
*Prepare file for Maps
****************************************************
****************************************************

preserve
collapse (sum) FDI_stock_jobs formal_jobs1 informal_jobs1 non_FDI_jobs1 total_jobs people , by(district_name year)
encode district_name, gen(id)
xtset id year
gen period=2
replace period=1 if year==2002
xtset id period
foreach x in FDI_stock_jobs formal_jobs1 informal_jobs1 non_FDI_jobs1 total_jobs people{
gen diff`x'=d.`x'
}

drop id
drop year
reshape wide formal_jobs1 informal_jobs1 non_FDI_jobs1 total_jobs people FDI_stock_jobs diffformal_jobs1 diffinformal_jobs1 ///
diffnon_FDI_jobs1 difftotal_jobs diffpeople diffFDI_stock_jobs, i(district_name ) j(period)
drop diffformal_jobs11 diffinformal_jobs11 diffnon_FDI_jobs11 difftotal_jobs1 diffpeople1 diffFDI_stock_jobs1
rename district_name district
save mapstuff_, replace
*the file with real district names --> mapstuff.dta
restore


****************************************************
****************************************************
*TABLE in FIGURE 8
****************************************************
****************************************************

foreach x in informal_jobs1  formal_jobs1  FDI_stock_jobs {
gen m`x'=`x'/1000000
}
gen jobs_created_by_FDI=(5.28*FDI_stock_jobs)/1000000
gen non_created_non_FDI_jobs=(non_FDI_jobs1-jobs_created_by_FDI)/1000000
bys province: egen rank1=sum(jobs_created_by_FDI+non_created_non_FDI_jobs) if year==2014 
bys sectorname: egen rank2=sum(jobs_created_by_FDI+non_created_non_FDI_jobs) if year==2014 


*province names anonymised
graph hbar (sum) non_created_non_FDI_jobs (sum) jobs_created_by_FDI if year==2014   ,  stack  scheme(Plotplainblind) ///
over(province, sort(rank1) descending)   ///
legend(order(2 "Share due to FDI") pos(6)) ///
title(Jobs by province (millions)) xsize(4) ysize(5) 
graph export "FDI_due_jobs_2014_.pdf", as(pdf) replace
****************************************************
****************************************************

****************************************************
****************************************************
*TABLE in FIGURE A.7
****************************************************
****************************************************

*province names anonymised
*descriptives by sector and province
gen temp3=informal_jobs1 + formal_jobs1 + FDI_stock_jobs if year==2014
bys province: egen province_jobs=sum(temp3) 

gen bin=mformal_jobs1+mFDI_stock_jobs 
graph hbar (sum) minformal_jobs1 (sum) bin if year==2014   ,  stack  scheme(Plotplainblind) over(province, sort(province_jobs) descending) legend(lab(1 "Informal") lab(2 "Formal") pos(6) ) title("Informal and formal jobs (millions)") xsize(4) ysize(5) 
graph export "sharejobs_2014_.pdf", as(pdf) replace
graph hbar (sum) formal_jobs1 (sum) FDI_stock_jobs if year==2014   ,  stack  scheme(Plotplainblind) over(province, sort(mformal_jobs1) descending) legend(lab(1 "Local jobs") lab(2 "FDI jobs") pos(6) ) title("Formal jobs") xsize(4) ysize(5) 
graph export "shareformaljobs_2014_.pdf", as(pdf) replace
bys sectorname: egen rank3=sum(formal_jobs1+FDI_stock_jobs) if year==2014 
bys sectorname: egen rank4=sum(minformal_jobs1+bin) if year==2014 
graph hbar (sum) minformal_jobs1 (sum) bin if year==2014  & sectorname!="Unemployment" ,  stack  scheme(Plotplainblind) over(sectorname, sort(rank4) descending) legend(lab(1 "Informal") lab(2 "Formal") pos(6) ) title("Informal and formal jobs (millions)") xsize(4) ysize(5) 
graph export "ssharejobs_2014_.pdf", as(pdf) replace
graph hbar (sum) formal_jobs1 (sum) FDI_stock_jobs if year==2014 & sectorname!="Unemployment"  ,  stack  scheme(Plotplainblind) over(sectorname, sort(rank3) descending) legend(lab(1 "Local jobs") lab(2 "FDI jobs") pos(6) ) title("Formal jobs") xsize(4) ysize(5) 
graph export "sshareformaljobs_2014_.pdf", as(pdf) replace

xtset district_sector period_two
foreach x in FDI_stock_jobs total_jobs{
gen ln`x'=ln(`x'+1)
gen dln`x'=d.ln`x'
}

*keep district, sector, year cobintation with HH data presence
keep if HH_yes==1

eststo clear
lab var non_FDI_jobs1  "Non-FDI jobs"
lab var non_FDI_jobs3 "Non-FDI jobs"
lab var formal_jobs1 "Formal jobs"
lab var formal_jobs3 "Formal jobs"
lab var informal_jobs1 "Informal jobs"
lab var informal_jobs3 "Informal jobs"
lab var FDI_stock_jobs "FDI jobs (CEMPRE)"
lab var cum_jobs "FDI jobs (FT)"
lab var FDI_stock "FDI projects (CEMPRE)"
lab var cum_projects "FDI projects (FT)"
lab var total_jobs  "Total jobs"
lab var men "Men employed"
lab var women "Women employed"
lab var educated  "Skilled employed"
lab var noteducated "Unskilled employed"

foreach x in FDI_stock_jobs cum_jobs cum_projects FDI_stock {
bys  district period: egen sum`x'=sum(`x')
gen Sp`x'=sum`x'-`x'
} 

lab var SpFDI_stock_jobs "FDI jobs in other sectors (CEMPRE)"
lab var Spcum_jobs "FDI jobs in other sectors (FT)"
lab var SpFDI_stock "FDI projects in other sectors (CEMPRE)"
lab var Spcum_projects "FDI projects in other sectors (FT)"

foreach x in FDI_stock_jobs cum_jobs cum_projects FDI_stock {
gen Mfg`x'=`x' if sectorname=="Manufacturing"
gen Ser`x'=`x' if sectorname=="Services"
bys  district period: egen mfg`x'=sum(Mfg`x')
bys  district period: egen ser`x'=sum(Ser`x')
} 

lab var mfgFDI_stock_jobs "FDI jobs in manufacturing (CEMPRE)"
lab var mfgcum_jobs "FDI jobs in manufacturing  (FT)"
lab var mfgFDI_stock "FDI projects in manufacturing  (CEMPRE)"
lab var mfgcum_projects "FDI projects manufacturing  (FT)"

lab var serFDI_stock_jobs "FDI jobs in services (CEMPRE)"
lab var sercum_jobs "FDI jobs in services  (FT)"
lab var serFDI_stock "FDI projects in services  (CEMPRE)"
lab var sercum_projects "FDI projects services  (FT)"

drop _merge
save clean_hh_firms_FDI_ano2, replace



use clean_hh_firms_FDI_ano2, clear
gen cum_jobs2014=cum_jobs if period==3
gen FDI_stock_jobs2014=FDI_stock_jobs if period==3
egen total_MOZ_FDI_2014_FT=sum(cum_jobs2014)
egen total_MOZ_FDI_2014_CEMPRE=sum(FDI_stock_jobs2014)

****************************************************
****************************************************

****************************************************
****************************************************
*TABLE A.9
*Triple Differneces
****************************************************
****************************************************

eststo clear
eststo: reghdfe non_FDI_jobs1   FDI_stock_jobs if alternativesector>-1, absorb(year_sector district_sector  dist_period)  cl(provdist  alternativesector) 
eststo: reghdfe non_FDI_jobs3  cum_jobs if alternativesector>-1, absorb(year_sector district_sector dist_period)  cl(provdist  alternativesector) 
eststo: reghdfe formal_jobs1   FDI_stock_jobs if alternativesector>-1, absorb(year_sector district_sector  dist_period)  cl(provdist  alternativesector) 
eststo: reghdfe formal_jobs3  cum_jobs if alternativesector>-1, absorb(year_sector district_sector dist_period)  cl(provdist  alternativesector) 
eststo: reghdfe informal_jobs1   FDI_stock_jobs if alternativesector>-1, absorb(year_sector district_sector  dist_period)  cl(provdist  alternativesector) 
eststo: reghdfe informal_jobs3  cum_jobs if alternativesector>-1, absorb(year_sector district_sector dist_period)  cl(provdist  alternativesector) 
esttab using new_deal11.tex, replace label nogaps se b(%9.3f) star(* 0.10 ** 0.05 *** 0.01) stats(N r2, fmt(%9.0g %9.2f %9.2f %9.2f %9.2f) labels(N "R-sq")) nonote


****************************************************
****************************************************

****************************************************
****************************************************
*TABLE A.10
***with spillovers
****************************************************
****************************************************

eststo clear
eststo: reghdfe non_FDI_jobs1   FDI_stock_jobs SpFDI_stock_jobs if alternativesector>-1, absorb(year_sector district_sector  )  cl(provdist  alternativesector) 
eststo: reghdfe non_FDI_jobs3  cum_jobs Spcum_jobs if alternativesector>-1, absorb(year_sector district_sector )  cl(provdist  alternativesector) 
eststo: reghdfe formal_jobs1   FDI_stock_jobs SpFDI_stock_jobs if alternativesector>-1, absorb(year_sector district_sector  )  cl(provdist  alternativesector) 
eststo: reghdfe formal_jobs3  cum_jobs Spcum_jobs if alternativesector>-1, absorb(year_sector district_sector )  cl(provdist  alternativesector) 
eststo: reghdfe informal_jobs1   FDI_stock_jobs SpFDI_stock_jobs if alternativesector>-1, absorb(year_sector district_sector  )  cl(provdist  alternativesector) 
eststo: reghdfe informal_jobs3  cum_jobs Spcum_jobs if alternativesector>-1, absorb(year_sector district_sector )  cl(provdist  alternativesector) 
esttab using spillovers_NEW.tex, replace label nogaps se b(%9.3f) star(* 0.10 ** 0.05 *** 0.01) stats(N r2, fmt(%9.0g %9.2f %9.2f %9.2f %9.2f) labels(N "R-sq")) nonote


****************************************************
****************************************************

****************************************************
****************************************************
*FIGURE A.11
*placebo discoveries within discovery countries
*ivCEMPRE_jobs
****************************************************
****************************************************


preserve 
set seed 1111
forvalues i = 1(1)100 {
shufflevar FDI_stock_jobs, cluster(dist_period)
gen  xFDI_stock_jobs_shuffled`i'=FDI_stock_jobs_shuffled
drop FDI_stock_jobs_shuffled
shufflevar FDI_stock_jobs, cluster(year_sector)
gen  FDI_stock_jobs_shuffled`i'=FDI_stock_jobs_shuffled
drop FDI_stock_jobs_shuffled
reghdfe non_FDI_jobs1   FDI_stock_jobs_shuffled`i'  if alternativesector>-1, absorb(year_sector district_sector  dist_period)  cl(provdist  alternativesector) 
matrix b=e(b)
matrix V=e(V)
scalar b=b[1,1]
scalar varb=V[1,1]
gen beta`i'=b
gen se`i'=sqrt(varb)
gen upper`i'=beta`i'+1.645*se`i'
gen lower`i'=beta`i'-1.645*se`i'
reghdfe non_FDI_jobs1   xFDI_stock_jobs_shuffled`i'  if alternativesector>-1, absorb(year_sector district_sector  dist_period)  cl(provdist  alternativesector)
matrix b=e(b)
matrix V=e(V)
scalar b=b[1,1]
scalar varb=V[1,1]
gen xbeta`i'=b
gen xse`i'=sqrt(varb)
gen xupper`i'=xbeta`i'+1.645*xse`i'
gen xlower`i'=xbeta`i'-1.645*xse`i'
}
keep xbeta* beta*  upper* lower*  xupper* xlower*  se* xse*
gen trend=_n
keep if trend==1
reshape long xbeta beta upper lower  xupper xlower  se xse, i(trend) j(emp) 
replace trend=_n
twoway (kdensity beta ) (kdensity xbeta ) , title("") xtitle(FDI job multiplier) xline(6.228) ///
legend(label(1 "100 shuffles within sector-year") lab(2 "100 shuffles within district-year")  pos(6) ) ///
xsize(4) ysize(4) scheme(Plotplainblind) xlabel(-2(2)8) text(1 7.2 "Baseline") text(0.94 7.2 "Estimate")
graph export shuffle_puck_.pdf, as(pdf) replace
restore


****************************************************
****************************************************

****************************************************
****************************************************
* FIGURE A.8
* anonymised province names
* compare the 2 FDI datasets
****************************************************
****************************************************


use compare_FDI_data.dta, clear

*porjects require assumption of no survivorship bias (the dead are missing) but also no M&A (we inlcude too many as FDI)
graph hbar (sum) projects_fDi FDI_firms  if sectorname=="Services"  & year>2002 & year<2015, over(  Província ) scheme(Plotplainblind) xsize(8) ysize(6) title(FDI projects in Services (2003-2014)) legend(label(1 "fDi Markets") lab(2 "Firm census") ring(0)) 
graph export "bar_services_.pdf", as(pdf) replace
graph hbar (sum) projects_fDi FDI_firms  if sectorname=="Manufacturing"  & year>2002 & year<2015, over(  Província ) scheme(Plotplainblind) xsize(8) ysize(6) title(FDI projects in Manufacturing (2003-2014)) legend(label(1 "fDi Markets") lab(2 "Firm census") ring(0))  
graph export "bar_manufacturing_.pdf", as(pdf) replace
*jobs requires more assumptions (no job growth) so the job numbers are inflated.
graph hbar (sum) jobs_fDi FDI_jobs  if sectorname=="Services"  & year>2002 & year<2015, over(  Província ) scheme(Plotplainblind) xsize(8) ysize(6) title(FDI jobs in Services (2003-2014)) legend(label(1 "fDi Markets") lab(2 "Firm census") ring(0)) 
graph export "jbar_services_.pdf", as(pdf) replace
graph hbar (sum) jobs_fDi FDI_jobs  if sectorname=="Manufacturing"  & year>2002 & year<2015, over(  Província ) scheme(Plotplainblind) xsize(8) ysize(6) title(FDI jobs in  Manufacturing (2003-2014)) legend(label(1 "fDi Markets") lab(2 "Firm census") ring(0))  
graph export "jbar_manufacturing_.pdf", as(pdf) replace

collapse (sum) projects_fDi value_fDi jobs_fDi FDI_firms FDI_jobs local_jobs local_firms , by(sectorname year )
egen id=group( sectorname)
xtset id year
lab var projects_fDi "fDi Markets"
lab var FDI_firms "CEMPRE"
lab var jobs_fDi "fDi Markets"
lab var FDI_jobs "CEMPRE"
lab var local_jobs "CEMPRE"
lab var local_firms "CEMPRE"  
twoway (line projects_fDi year) (line FDI_firms year) if sectorname=="Manufacturing"  & year<2015 ,  scheme(Plotplainblind) xsize(8) ysize(6) title(Manufacturing) xtitle("") ytitle(FDI projects) legend(label(1 "fDi Markets") lab(2 "Firm census") pos(11) ring(0))  
graph export "line_manufacturing_.pdf", as(pdf) replace
twoway (line projects_fDi year) (line FDI_firms year) if sectorname=="Services"  & year<2015 ,  scheme(Plotplainblind) xsize(8) ysize(6) title(Services)  xtitle("") ytitle(FDI projects) legend(label(1 "fDi Markets") lab(2 "Firm census") pos(11) ring(0))  
graph export "line_services_.pdf", as(pdf) replace
twoway (line jobs_fDi year) (line  FDI_jobs year) if sectorname=="Manufacturing"  & year<2015 ,  scheme(Plotplainblind) xsize(8) ysize(6) title(Manufacturing) xtitle("") ytitle(FDI jobs) legend(label(1 "fDi Markets") lab(2 "Firm census") pos(11) ring(0))  
graph export "jline_manufacturing_.pdf", as(pdf) replace
twoway (line jobs_fDi year) (line  FDI_jobs year) if sectorname=="Services"  & year<2015 ,  scheme(Plotplainblind) xsize(8) ysize(6) title(Services)  xtitle("") ytitle(FDI jobs) legend(label(1 "fDi Markets") lab(2 "Firm census") pos(11) ring(0))  
graph export "jline_services_.pdf", as(pdf) replace

****************************************************
****************************************************

****************************************************
****************************************************
*MAPS 
****************************************************
****************************************************

use  moz_map2, clear
gen district=subinstr( DISTRICT,"Cidade De ","",1)
replace district=subinstr( district,"Cidade Da ","",1)
replace district=subinstr( district,"Cidade de ","",1)
replace district="Mandlakazi" if district=="Mandlakaze/Manjacaze"
replace district="Nacala-Velha" if district=="Nacala-A-Velha"
replace district="Mocambique" if district=="Ilha De Mozambique"
replace district="Praia" if district=="Mocimboa Da Praia"
replace district="Zumbo" if district=="Zumbu"
replace district="Cahora-Bassa" if district=="Cahora Bassa"
replace district="Bilene-Macia" if district=="Bilene"
replace district="Costa" if district=="Maganja Da Costa"
replace district="Molocue" if district=="Alto Molocue"
replace district="Lago" if district=="Lago Niassa"
merge m:1 district using "mapstuff_multi.dta"
drop if _merge==2
drop _merge

****************************************************
****************************************************

****************************************************
****************************************************
*FIGURE 8 
*map
****************************************************
****************************************************

spmap  snfdijobsduetofdi using MOZ_adm_D , id(_ID) fcolor(Heat) ndfcolor(gs16) cln(16)  title("Share of 2014 non-FDI jobs" "due to 2003-2014 FDI") legend(pos(5) lab(1 "No data") size(large))
graph export "moz_fdi_jobs_map3_.pdf", as(pdf)  replace


****************************************************
****************************************************

****************************************************
****************************************************
*FIGURE A.6 maps
* can't reproduce due to anonymosity 
****************************************************
****************************************************
/*
use  moz_map2, clear
gen district=subinstr( DISTRICT,"Cidade De ","",1)
replace district=subinstr( district,"Cidade Da ","",1)
replace district=subinstr( district,"Cidade de ","",1)
replace district="Mandlakazi" if district=="Mandlakaze/Manjacaze"
replace district="Nacala-Velha" if district=="Nacala-A-Velha"
replace district="Mocambique" if district=="Ilha De Mozambique"
replace district="Praia" if district=="Mocimboa Da Praia"
replace district="Zumbo" if district=="Zumbu"
replace district="Cahora-Bassa" if district=="Cahora Bassa"
replace district="Bilene-Macia" if district=="Bilene"
replace district="Costa" if district=="Maganja Da Costa"
replace district="Molocue" if district=="Alto Molocue"
replace district="Lago" if district=="Lago Niassa"

*this is the file we are not providing
merge m:1 district using "census_for_maps2.dta"
drop if _merge==2
drop _merge
forvalues i = 2000(1)2014 {
replace CFDI_firms`i'=0 if CFDI_firms`i'==.
}

forvalues i = 2000(1)2014 {
replace CFDI_firms`i'=. if CFDI_firms`i'==0
}

forvalues i = 2002(12)2014 {
spmap  CFDI_firms`i' using MOZ_adm_D , id(_ID) fcolor(Heat) ndfcolor(gs12) cln(12) ///
 title(`i') legend(pos(5) lab(1 "0") size(large))
graph export "moz_fdi_map`i'.pdf", as(pdf) replace
}
*/
****************************************************
****************************************************

****************************************************
****************************************************
*building the IV - city
****************************************************
****************************************************

use shareinvet.dta, clear

*get the city ranking data from worldatlas
*http://www.worldatlas.com/af/mz/where-is-mozambique.html				
				
gen city_rank=0
replace city_rank=1 if DestinationCity=="Maputo" & DestinationCountry=="Mozambique"
replace city_rank=2 if DestinationCity=="Matola"&DestinationCountry=="Mozambique"
replace city_rank=3 if DestinationCity=="Beira"&DestinationCountry=="Mozambique"
replace city_rank=4 if DestinationCity=="Nampula"&DestinationCountry=="Mozambique"
replace city_rank=5 if DestinationCity=="Chimoio"&DestinationCountry=="Mozambique"
replace city_rank=6 if DestinationCity=="Nacala"&DestinationCountry=="Mozambique"
replace city_rank=7 if DestinationCity=="Quelimane"&DestinationCountry=="Mozambique"
replace city_rank=8 if DestinationCity=="Tete"&DestinationCountry=="Mozambique"
replace city_rank=9 if DestinationCity=="Xai-Xai"&DestinationCountry=="Mozambique"
replace city_rank=10 if DestinationCity=="Maxixe"&DestinationCountry=="Mozambique"
replace city_rank=11 if DestinationCity=="Lichinga"&DestinationCountry=="Mozambique"
replace city_rank=12 if DestinationCity=="Pemba"&DestinationCountry=="Mozambique"	
replace city_rank=13 if DestinationCity=="Dondo"&DestinationCountry=="Mozambique"
replace city_rank=14 if DestinationCity=="Inhambane"&DestinationCountry=="Mozambique"	
replace city_rank=15 if DestinationCity=="Cuamba"&DestinationCountry=="Mozambique"	
drop if city_rank==0 & DestinationCountry=="Mozambique"
								 
*http://www.worldatlas.com/af/et/where-is-ethiopia.html
								 
replace city_rank=1 if DestinationCity=="Addis Ababa"&DestinationCountry=="Ethiopia"
replace city_rank=2 if DestinationCity=="Dire Dawa"&DestinationCountry=="Ethiopia"
replace city_rank=3 if DestinationCity=="Mek'ele"&DestinationCountry=="Ethiopia"
replace city_rank=4 if DestinationCity=="Nazret"&DestinationCountry=="Ethiopia"
replace city_rank=5 if DestinationCity=="Bahir Dar"&DestinationCountry=="Ethiopia"
replace city_rank=6 if DestinationCity=="Gondar"&DestinationCountry=="Ethiopia"
replace city_rank=7 if DestinationCity=="Dese"&DestinationCountry=="Ethiopia"
replace city_rank=8 if DestinationCity=="Hawassa"&DestinationCountry=="Ethiopia"
replace city_rank=9 if DestinationCity=="Jima"&DestinationCountry=="Ethiopia"
replace city_rank=10 if DestinationCity=="Bishoftu"&DestinationCountry=="Ethiopia"
replace city_rank=11 if DestinationCity=="Kombolcha"&DestinationCountry=="Ethiopia"
replace city_rank=12 if DestinationCity=="Harar"&DestinationCountry=="Ethiopia"
replace city_rank=13 if DestinationCity=="Shashemene"&DestinationCountry=="Ethiopia"	
replace city_rank=14 if DestinationCity=="Abra Minch"&DestinationCountry=="Ethiopia"
replace city_rank=15 if DestinationCity=="Adigrat"&DestinationCountry=="Ethiopia"			 

drop if city_rank==0 & DestinationCountry=="Ethiopia"

*http://www.worldatlas.com/af/et/where-is-ghana.html
								 
replace city_rank=1 if DestinationCity=="Accra"&DestinationCountry=="Ghana"
replace city_rank=2 if DestinationCity=="Kumasi"&DestinationCountry=="Ghana"
replace city_rank=3 if DestinationCity=="Tamale"&DestinationCountry=="Ghana"
replace city_rank=4 if DestinationCity=="Takoradi"&DestinationCountry=="Ghana"
replace city_rank=5 if DestinationCity=="Ashiaman"&DestinationCountry=="Ghana"
replace city_rank=6 if DestinationCity=="Tema"&DestinationCountry=="Ghana"
replace city_rank=7 if DestinationCity=="Teshi Old Town"&DestinationCountry=="Ghana"
replace city_rank=8 if DestinationCity=="Cape Coast"&DestinationCountry=="Ghana"
replace city_rank=9 if DestinationCity=="Sekondi-Takoradi"&DestinationCountry=="Ghana"
replace city_rank=10 if DestinationCity=="Obuasi"&DestinationCountry=="Ghana"
replace city_rank=11 if DestinationCity=="Medina Estates"&DestinationCountry=="Ghana"
replace city_rank=12 if DestinationCity=="Koforidua"&DestinationCountry=="Ghana"
replace city_rank=13 if DestinationCity=="Wa"&DestinationCountry=="Ghana"	
replace city_rank=14 if DestinationCity=="Ejura"&DestinationCountry=="Ghana"
replace city_rank=15 if DestinationCity=="Nungua"&DestinationCountry=="Ghana"			 

drop if city_rank==0 & DestinationCountry=="Ghana"

*https://en.wikipedia.org/wiki/List_of_cities_in_Tanzania
								 
replace city_rank=1 if DestinationCity=="Dar es Salaam"&DestinationCountry=="Tanzania"
replace city_rank=2 if DestinationCity=="Mwanza"&DestinationCountry=="Tanzania"
replace city_rank=3 if DestinationCity=="Arusha"&DestinationCountry=="Tanzania"
replace city_rank=4 if DestinationCity=="Dodoma"&DestinationCountry=="Tanzania"
replace city_rank=5 if DestinationCity=="Mbeya"&DestinationCountry=="Tanzania"
replace city_rank=6 if DestinationCity=="Morogoro"&DestinationCountry=="Tanzania"
replace city_rank=7 if DestinationCity=="Tanga"&DestinationCountry=="Tanzania"
replace city_rank=8 if DestinationCity=="Kahama"&DestinationCountry=="Tanzania"
replace city_rank=9 if DestinationCity=="Tabora"&DestinationCountry=="Tanzania"
replace city_rank=10 if DestinationCity=="Zanzibar"&DestinationCountry=="Tanzania"
replace city_rank=11 if DestinationCity=="Kigoma"&DestinationCountry=="Tanzania"
replace city_rank=12 if DestinationCity=="Sumbawanga"&DestinationCountry=="Tanzania"
replace city_rank=13 if DestinationCity=="Kasulu"&DestinationCountry=="Tanzania"	
replace city_rank=14 if DestinationCity=="Songea"&DestinationCountry=="Tanzania"
replace city_rank=15 if DestinationCity=="Moshi"&DestinationCountry=="Tanzania"	
		 
drop if city_rank==0 & DestinationCountry=="Tanzania"

collapse (sum) projects InvestmentUSm JobsCreated (mean) projectstotal investmenttotal jobstotal city_rank, by( DestinationCountry DestinationCity)

by DestinationCountry: egen suminvestment=sum( InvestmentUSm)
by DestinationCountry: egen sumjobs=sum( JobsCreated)

egen alljobs=sum(JobsCreated) if DestinationCountry!="Mozambique"
*construct weights for the IV
gen weight=sumjobs/alljobs

*check to what they add up
gen investshare=suminvestment/investmenttotal
gen jobshare=sumjobs/jobstotal

gen share_jobs= JobsCreated/ jobstotal
gen share_invest= InvestmentUSm / investmenttotal
gen share_projects= projects / projectstotal

fillin DestinationCountry city_rank
replace share_jobs=0 if _fillin==1
replace share_invest=0 if _fillin==1
sort  DestinationCountry city_rank

egen id=group(DestinationCountry)
xtset id city_rank

gen cum_share=share_jobs if city_rank==1
bys id: replace cum_share=share_jobs+cum_share[_n-1] if _n>1

gen cum_invest=share_invest if city_rank==1
bys id: replace cum_invest=share_invest+cum_invest[_n-1] if _n>1


****************************************************
****************************************************

****************************************************
****************************************************
*FIGURE A.9 top
****************************************************
****************************************************

twoway (line cum_share city_rank if DestinationCountry=="Ethiopia") (line cum_share city_rank if DestinationCountry=="Ghana") ///
(line cum_share city_rank if DestinationCountry=="Mozambique") (line cum_share city_rank if DestinationCountry=="Tanzania"), ///
scheme(Plotplainblind) xtitle(City rank by population) ytitle(Cumulative share of FDI jobs) xsize(4.5) ysize(4) ///
legend(lab(1 "Ethiopia") lab(2 "Ghana") lab(3 "Mozambique") lab(4 "Tanzania") pos(5) ring(0))
graph export "share_jobs_ranks_.pdf", as(pdf)  replace

drop if DestinationCountry=="Mozambique"

gen share_jobs_weight=weight*share_jobs
gen share_invest_weight=weight*share_invest

collapse (mean) share_jobs share_invest share_projects (sum) share_invest_weight share_jobs_weight, by(city_rank)

save city_rank_IV, replace

****************************************************
****************************************************

****************************************************
****************************************************
*building the IV - sector
****************************************************
****************************************************

clear
use shareinvet.dta

collapse (sum) projects InvestmentUSm JobsCreated (mean) projectstotal investmenttotal jobstotal, by( DestinationCountry BusinessActivity)

gen hh=""
rename BusinessActivity  industryactivity
replace hh="Construction" if industryactivity=="Construction"
replace hh="Manufacturing" if industryactivity=="Design, Development & Testing"
replace hh="Extraction" if industryactivity=="Extraction"
replace hh="Manufacturing" if industryactivity=="Manufacturing"
replace hh="Transportation" if industryactivity=="Logistics, Distribution & Transportation"
replace hh="Services" if industryactivity=="Business Services"
replace hh="Services" if industryactivity=="Retail"
replace hh="Services" if industryactivity=="Maintenance & Servicing"
replace hh="Services" if industryactivity=="Headquarters"
replace hh="Services" if industryactivity=="ICT & Internet Infrastructure"
replace hh="Services" if industryactivity=="Sales, Marketing & Support"
replace hh="Services" if industryactivity=="Electricity"

rename hh sectorname
drop if sectorname==""

collapse (sum) projects InvestmentUSm JobsCreated (mean) projectstotal investmenttotal jobstotal, by( DestinationCountry sectorname)
by DestinationCountry: egen sumFDI=sum( InvestmentUSm)
by DestinationCountry: egen sumFDIjobs=sum( JobsCreated)
by DestinationCountry: egen sumFDIprojects=sum(projects)

gen share_jobs_sector= JobsCreated/ jobstotal
gen share_invest_sector=InvestmentUSm/  investmenttotal 
gen share_projects_sector=projects/  projectstotal


****************************************************
****************************************************

****************************************************
****************************************************
*FIGURE A.9 bottom
****************************************************
****************************************************

graph hbar (asis) share_jobs_sector, over(sectorname) by(DestinationCountry, note("") title(Share of FDI jobs)) scheme(Plotplainblind)
graph export "share_jobs_sectors_.pdf", as(pdf)  replace

drop if DestinationCountry=="Mozambique"

egen alljobs=sum(JobsCreated)
gen weight=sumFDIjobs/alljobs
gen share_jobs_sector_weight=weight*share_jobs
gen share_invest_sector_weight=weight*share_invest

foreach x in JobsCreated{
gen `x'_w=weight*`x'
}

collapse (mean) share_jobs_sector share_invest_sector share_projects_sector JobsCreated_w  JobsCreated  (sum)  share_invest_sector_weight share_jobs_sector_weight , by(sectorname)
save sector_rank_IV, replace


****************************************************
****************************************************
*building the IV - sector - city
****************************************************
****************************************************

clear
use shareinvet.dta

drop if DestinationCountry=="Mozambique"

gen hh=""
rename BusinessActivity  industryactivity
replace hh="Construction" if industryactivity=="Construction"
replace hh="Manufacturing" if industryactivity=="Design, Development & Testing"
replace hh="Extraction" if industryactivity=="Extraction"
replace hh="Manufacturing" if industryactivity=="Manufacturing"
replace hh="Transportation" if industryactivity=="Logistics, Distribution & Transportation"
replace hh="Services" if industryactivity=="Business Services"
replace hh="Services" if industryactivity=="Retail"
replace hh="Services" if industryactivity=="Maintenance & Servicing"
replace hh="Services" if industryactivity=="Headquarters"
replace hh="Services" if industryactivity=="ICT & Internet Infrastructure"
replace hh="Services" if industryactivity=="Sales, Marketing & Support"
replace hh="Services" if industryactivity=="Electricity"

rename hh sectorname
drop if sectorname==""

		
gen city_rank=0
*http://www.worldatlas.com/af/et/where-is-ethiopia.html
								 
replace city_rank=1 if DestinationCity=="Addis Ababa"&DestinationCountry=="Ethiopia"
replace city_rank=2 if DestinationCity=="Dire Dawa"&DestinationCountry=="Ethiopia"
replace city_rank=3 if DestinationCity=="Mek'ele"&DestinationCountry=="Ethiopia"
replace city_rank=4 if DestinationCity=="Nazret"&DestinationCountry=="Ethiopia"
replace city_rank=5 if DestinationCity=="Bahir Dar"&DestinationCountry=="Ethiopia"
replace city_rank=6 if DestinationCity=="Gondar"&DestinationCountry=="Ethiopia"
replace city_rank=7 if DestinationCity=="Dese"&DestinationCountry=="Ethiopia"
replace city_rank=8 if DestinationCity=="Hawassa"&DestinationCountry=="Ethiopia"
replace city_rank=9 if DestinationCity=="Jima"&DestinationCountry=="Ethiopia"
replace city_rank=10 if DestinationCity=="Bishoftu"&DestinationCountry=="Ethiopia"
replace city_rank=11 if DestinationCity=="Kombolcha"&DestinationCountry=="Ethiopia"
replace city_rank=12 if DestinationCity=="Harar"&DestinationCountry=="Ethiopia"
replace city_rank=13 if DestinationCity=="Shashemene"&DestinationCountry=="Ethiopia"	
replace city_rank=14 if DestinationCity=="Abra Minch"&DestinationCountry=="Ethiopia"
replace city_rank=15 if DestinationCity=="Adigrat"&DestinationCountry=="Ethiopia"			 

drop if city_rank==0 & DestinationCountry=="Ethiopia"

*http://www.worldatlas.com/af/et/where-is-ghana.html
								 
replace city_rank=1 if DestinationCity=="Accra"&DestinationCountry=="Ghana"
replace city_rank=2 if DestinationCity=="Kumasi"&DestinationCountry=="Ghana"
replace city_rank=3 if DestinationCity=="Tamale"&DestinationCountry=="Ghana"
replace city_rank=4 if DestinationCity=="Takoradi"&DestinationCountry=="Ghana"
replace city_rank=5 if DestinationCity=="Ashiaman"&DestinationCountry=="Ghana"
replace city_rank=6 if DestinationCity=="Tema"&DestinationCountry=="Ghana"
replace city_rank=7 if DestinationCity=="Teshi Old Town"&DestinationCountry=="Ghana"
replace city_rank=8 if DestinationCity=="Cape Coast"&DestinationCountry=="Ghana"
replace city_rank=9 if DestinationCity=="Sekondi-Takoradi"&DestinationCountry=="Ghana"
replace city_rank=10 if DestinationCity=="Obuasi"&DestinationCountry=="Ghana"
replace city_rank=11 if DestinationCity=="Medina Estates"&DestinationCountry=="Ghana"
replace city_rank=12 if DestinationCity=="Koforidua"&DestinationCountry=="Ghana"
replace city_rank=13 if DestinationCity=="Wa"&DestinationCountry=="Ghana"	
replace city_rank=14 if DestinationCity=="Ejura"&DestinationCountry=="Ghana"
replace city_rank=15 if DestinationCity=="Nungua"&DestinationCountry=="Ghana"			 

drop if city_rank==0 & DestinationCountry=="Ghana"

*https://en.wikipedia.org/wiki/List_of_cities_in_Tanzania
								 
replace city_rank=1 if DestinationCity=="Dar es Salaam"&DestinationCountry=="Tanzania"
replace city_rank=2 if DestinationCity=="Mwanza"&DestinationCountry=="Tanzania"
replace city_rank=3 if DestinationCity=="Arusha"&DestinationCountry=="Tanzania"
replace city_rank=4 if DestinationCity=="Dodoma"&DestinationCountry=="Tanzania"
replace city_rank=5 if DestinationCity=="Mbeya"&DestinationCountry=="Tanzania"
replace city_rank=6 if DestinationCity=="Morogoro"&DestinationCountry=="Tanzania"
replace city_rank=7 if DestinationCity=="Tanga"&DestinationCountry=="Tanzania"
replace city_rank=8 if DestinationCity=="Kahama"&DestinationCountry=="Tanzania"
replace city_rank=9 if DestinationCity=="Tabora"&DestinationCountry=="Tanzania"
replace city_rank=10 if DestinationCity=="Zanzibar"&DestinationCountry=="Tanzania"
replace city_rank=11 if DestinationCity=="Kigoma"&DestinationCountry=="Tanzania"
replace city_rank=12 if DestinationCity=="Sumbawanga"&DestinationCountry=="Tanzania"
replace city_rank=13 if DestinationCity=="Kasulu"&DestinationCountry=="Tanzania"	
replace city_rank=14 if DestinationCity=="Songea"&DestinationCountry=="Tanzania"
replace city_rank=15 if DestinationCity=="Moshi"&DestinationCountry=="Tanzania"	
		 
drop if city_rank==0 & DestinationCountry=="Tanzania"


collapse (sum) projects JobsCreated InvestmentUSm, by(city_rank sectorname)

rename projects projects_IV 
rename InvestmentUSm InvestmentUSm_IV
rename JobsCreated JobsCreated_IV

save city_rank_sector_IV.dta, replace



****************************************************
****************************************************

****************************************************
****************************************************
*Baseline and IV regressions
****************************************************
****************************************************
use clean_hh_firms_FDI_ano2.dta, clear

*assign the city level city rank
gen city_rank=0
replace city_rank=1 if province==11
replace city_rank=2 if province==10&district==1
replace city_rank=3 if province==7&district==1
replace city_rank=4 if province==3&district==1
replace city_rank=5 if province==6&district==1
replace city_rank=6 if province==3&(district==17)|province==3&(district==18)
replace city_rank=7 if province==4&(district==1)
replace city_rank=8 if province==5&(district==1)
replace city_rank=9 if province==9&(district==1)
replace city_rank=10 if province==8&(district==10)
replace city_rank=11 if province==1&(district==1)
replace city_rank=12 if province==2&(district==1)
replace city_rank=13 if province==7&(district==7)
replace city_rank=14 if province==8&(district==1)
replace city_rank=15 if province==1&(district==2)
	
merge m:1 city_rank using "city_rank_IV.dta"
drop _merge

merge m:1 sectorname using "sector_rank_IV.dta"
drop _merge

merge m:1 city_rank sectorname using "city_rank_sector_IV.dta"
drop _merge

foreach x in share_jobs share_invest share_jobs_sector share_invest_sector JobsCreated ///
share_jobs_weight share_invest_weight share_jobs_sector_weight ///
share_projects_sector share_projects share_invest_sector_weight projects_IV InvestmentUSm_IV JobsCreated_IV{
replace `x'=0 if `x'==.
}

foreach x in projects_IV InvestmentUSm_IV JobsCreated_IV JobsCreated{
replace `x'=0 if year==2002
}

bys year: egen total_cum_projects=sum(cum_projects)


****************************************************
****************************************************
*Create alternative IVs
****************************************************
****************************************************

*Simple shares based on investmens in other African countries
gen ivCEMPRE_census=0
replace ivCEMPRE_census=share_jobs_sector*share_jobs*100000  if year==2014
lab var ivCEMPRE_census "Instrument"

*Pull and Push
bys alternativesector year: egen sum_FDI_stock_jobs=sum(FDI_stock_jobs)
bys provdist year: egen sum2_FDI_stock_jobs=sum(FDI_stock_jobs)
gen push=sum_FDI_stock_jobs-FDI_stock_jobs
gen pull=sum2_FDI_stock_jobs-FDI_stock_jobs
gen pushpull=push*pull

bys provdist year: egen sumfdijobs_d=sum(total_jobs)
gen sector_share_d=total_jobs/sumfdijobs_d
replace sector_share_d=. if year==2014
bys district_sector: egen shares=mean(sector_share_d)

gen eu1=(shares- share_jobs_sector)^2
bys provdist: egen euclid0=sum(eu1)
gen euclid = sqrt(euclid0)

*gen eu12=abs(shares- share_jobs_sector)
*bys provdist: egen manhattan=sum(eu12)

*gen eu13=abs(shares -share_jobs_sector)/(abs(shares)+abs(share_jobs_sector))
*bys provdist: egen canberra=sum(eu13)

gen card=shares*sum_FDI_stock_jobs

gen badass=JobsCreated_IV/euclid

gen sweet=shares*JobsCreated


****************************************************
****************************************************
*TRANSFORM TO CITY LEVEL
****************************************************
****************************************************

gen temp=people if alternativesector==-1
bys provdist year: egen unemployed=sum(temp)
gen temp2=total_jobs if alternativesector!=-1
bys provdist year: egen employed=sum(temp2)
bys provdist year: egen lf_city=sum(laborforce)
bys provdist year: egen people_city=sum(people)
bys provdist year: egen worker_city=sum(worker)
drop if alternativesector==-1
collapse (mean)  euclid share_jobs unemployed employed lf_city people_city worker_city (sum) ivCEMPRE_census total_jobs FDI_stock_jobs FDI_stock_jobs_adj2002 cum_jobs  FDI_stock FDI_stock_adj2002 ///
cum_projects  non_FDI_jobs1 non_FDI_jobs2 non_FDI_jobs3 informal_jobs1 informal_jobs2 informal_jobs3 formal_jobs1 formal_jobs2 formal_jobs3 ///
educated noteducated men women projects_IV InvestmentUSm_IV JobsCreated_IV card shares badass sweet pushpull, by(provdist year)
gen unemployment=100*((lf-worker)/lf)
gen participationrate=100*(lf/people)
gen nolf=people-lf
gen unemp=lf-worker

lab var non_FDI_jobs1  "Non-FDI jobs"
lab var non_FDI_jobs3 "Non-FDI jobs"
lab var formal_jobs1 "Formal jobs"
lab var formal_jobs3 "Formal jobs"
lab var informal_jobs1 "Informal jobs"
lab var informal_jobs3 "Informal jobs"
lab var FDI_stock_jobs "FDI jobs (CEMPRE)"
lab var cum_jobs "FDI jobs (FT)"
lab var FDI_stock "FDI projects (CEMPRE)"
lab var cum_projects "FDI projects (FT)"
lab var total_jobs  "Total jobs"
lab var men "Men employed"
lab var women "Women employed"
lab var educated  "Skilled employed"
lab var noteducated "Unskilled employed"
lab var unemployment "Unemployment rate"
lab var ivCEMPRE_census "Instrument"

****************************************************
****************************************************

****************************************************
****************************************************
*TABLE 2
****************************************************
****************************************************


set more off
eststo clear
foreach x in FDI_stock_jobs cum_jobs {
eststo clear
eststo: ivreghdfe total_jobs    `x' , absorb(provdist  year)  cl(provdist ) 
eststo: ivreghdfe non_FDI_jobs1   `x'  , absorb(provdist  year)  cl(provdist  ) 
eststo: ivreghdfe formal_jobs1   `x'  , absorb(provdist  year)  cl(provdist  )
eststo: ivreghdfe informal_jobs1   `x'  , absorb(provdist  year)  cl(provdist  ) 
esttab using city`x'.tex, replace label nogaps se b(%9.3f) star(* 0.10 ** 0.05 *** 0.01) stats(N r2, fmt(%9.0g %9.2f %9.2f %9.2f %9.2f) labels(N "R-sq")) nonote
}

 
****************************************************
****************************************************

****************************************************
****************************************************
*TABLE A.8 Panel A
****************************************************
****************************************************
*standardize the different IVs 
*1st IV
gen iv=badass
replace iv=0 if year==2002
sum iv
gen stdIV = 1000*(iv - r(mean)) / r(sd)
drop ivCEMPRE_census
clonevar ivCEMPRE_census=stdIV
replace ivCEMPRE_census=ivCEMPRE_census
lab var ivCEMPRE_census "Instrument"


eststo clear
foreach x in  FDI_stock_jobs   {
eststo clear
eststo: ivreghdfe `x'  ivCEMPRE_census  , absorb(provdist  year)  cl(provdist  ) 
eststo: ivreghdfe total_jobs    (`x'= ivCEMPRE_census ) , absorb(provdist  year)  cl(provdist)  ffirst
eststo: ivreghdfe non_FDI_jobs1  ( `x'= ivCEMPRE_census )  , absorb(provdist  year)  cl(provdist)  ffirst
eststo: ivreghdfe formal_jobs1   (`x'= ivCEMPRE_census )  , absorb(provdist  year)  cl(provdist) ffirst
eststo: ivreghdfe informal_jobs1 (  `x'= ivCEMPRE_census )  , absorb(provdist  year)  cl(provdist )  ffirst
esttab using city`x'_instrument.tex, replace label nogaps se b(%9.3f) star(* 0.10 ** 0.05 *** 0.01) stats(N r2 widstat, fmt(%9.0g %9.2f %9.2f %9.2f %9.2f) labels(N "R-sq" "F IV")) nonote
}

****************************************************
****************************************************

****************************************************
****************************************************
*FIGURE A.10 Panel A
****************************************************
****************************************************

*get the residuals
reghdfe formal_jobs1 , absorb(provdist  year) resid nocons
predict residy, resid 
reghdfe FDI_stock_jobs, absorb(provdist  year) resid nocons
predict residx, resid 
reghdfe ivCEMPRE_census, absorb(provdist  year) resid nocons
predict residz, resid 

*benchmark size of gamma
reg  residy residx , vce(robust)
ivreg  residy (residx = residz),  robust 
imperfectiv  residy (residx = residz), vce( robust )  
matrix LRbounds = e(LRbounds)
local lCI = LRbounds[1,1]
local uCI = LRbounds[1,4]
local LB  = LRbounds[1,2]
local UB  = LRbounds[1,3]


reghdfe FDI_stock_jobs    ivCEMPRE_census     , absorb(provdist  year)  nocons
local mval = 3* _b[ivCEMPRE_census]
foreach num of numlist 0(1)10 {
local om`num' = ((`num'/10)*`mval'/sqrt(12))^2
local mu`num' = (`num'/10)*`mval'/2
local d`num' = (`num'/10)*`mval'
    }
    local mvar = (`mval'/sqrt(12))^2
   
plausexog ltz residy (residx = residz),     omega(`mvar') mu(0) level(0.95) graph(residx) ///
graphomega(`om0' `om1' `om2' `om3' `om4' `om5' `om6' `om7' `om8' `om9' `om10') ///
graphmu(`mu0' `mu1' `mu2' `mu3' `mu4' `mu5' `mu6' `mu7' `mu8' `mu9' `mu10') ///
graphdelta(`d0' `d1' `d2' `d3' `d4' `d5' `d6' `d7' `d8' `d9' `d10')  ///
ytitle(IV estimate) xtitle({&delta}) vce(robust) scheme(Plotplainblind)  legend(off) 

graph export "IVrobust1_Remix_REStat_.pdf", as(pdf) replace

****************************************************
****************************************************

****************************************************
****************************************************
*TABLE A.8 Panel B
****************************************************
****************************************************
*standardize the different IVs 
*2nd IV

drop iv
drop ivCEMPRE_census
drop stdIV
replace card=0 if year==2002
sum card
gen stdIV = 1000*(card - r(mean)) / r(sd)
clonevar ivCEMPRE_census=stdIV
lab var ivCEMPRE_census "Instrument"

eststo clear
foreach x in  FDI_stock_jobs   {
eststo clear
eststo: ivreghdfe `x'  ivCEMPRE_census  , absorb(provdist  year)  cl(provdist  ) 
eststo: ivreghdfe total_jobs    (`x'= ivCEMPRE_census ) , absorb(provdist  year)  cl(provdist)  ffirst
eststo: ivreghdfe non_FDI_jobs1  ( `x'= ivCEMPRE_census )  , absorb(provdist  year)  cl(provdist)  ffirst
eststo: ivreghdfe formal_jobs1   (`x'= ivCEMPRE_census )  , absorb(provdist  year)  cl(provdist) ffirst
eststo: ivreghdfe informal_jobs1 (  `x'= ivCEMPRE_census )  , absorb(provdist  year)  cl(provdist )  ffirst
esttab using "city`x'_instrument_card.tex", replace label nogaps se b(%9.3f) star(* 0.10 ** 0.05 *** 0.01) stats(N r2 widstat, fmt(%9.0g %9.2f %9.2f %9.2f %9.2f) labels(N "R-sq" "F IV")) nonote
}

****************************************************
****************************************************

****************************************************
****************************************************
*FIGURE A.10 Panel B
****************************************************
****************************************************


drop residz
reghdfe ivCEMPRE_census     , absorb(provdist  year) resid nocons
predict residz, resid 

*benchmark size of gamma
reg  residy residx , vce( robust )
ivreg  residy (residx = residz) ,  robust 
imperfectiv  residy (residx = residz), vce( robust )  
matrix LRbounds = e(LRbounds)
local lCI = LRbounds[1,1]
local uCI = LRbounds[1,4]
local LB  = LRbounds[1,2]
local UB  = LRbounds[1,3]


reghdfe FDI_stock_jobs    ivCEMPRE_census     , absorb(provdist  year)  nocons
local mval = 3* _b[ivCEMPRE_census]
    foreach num of numlist 0(1)10 {
        local om`num' = ((`num'/10)*`mval'/sqrt(12))^2
        local mu`num' = (`num'/10)*`mval'/2
        local d`num' = (`num'/10)*`mval'
    }
    local mvar = (`mval'/sqrt(12))^2
   
    plausexog ltz residy (residx = residz),     omega(`mvar') mu(0) level(0.95) graph(residx) ///
    graphomega(`om0' `om1' `om2' `om3' `om4' `om5' `om6' `om7' `om8' `om9' `om10') ///
    graphmu(`mu0' `mu1' `mu2' `mu3' `mu4' `mu5' `mu6' `mu7' `mu8' `mu9' `mu10') ///
    graphdelta(`d0' `d1' `d2' `d3' `d4' `d5' `d6' `d7' `d8' `d9' `d10')  ///
    ytitle(IV estimate) xtitle({&delta}) vce(robust) scheme(Plotplainblind)  legend(off) 

graph export "IVrobust1_Remix_REStat_card_.pdf", as(pdf) replace





****************************************************
****************************************************

****************************************************
****************************************************
*TABLE A.8 Panel C
****************************************************
****************************************************
*standardize the different IVs 
*3rd IV

drop ivCEMPRE_census
drop stdIV
gen iv=sweet
sum iv
gen stdIV = 1000*(iv - r(mean)) / r(sd)
clonevar ivCEMPRE_census=stdIV
lab var ivCEMPRE_census "Instrument"

eststo clear
foreach x in  FDI_stock_jobs   {
eststo clear
eststo: ivreghdfe `x'  ivCEMPRE_census  , absorb(provdist  year)  cl(provdist  ) 
eststo: ivreghdfe total_jobs    (`x'= ivCEMPRE_census ) , absorb(provdist  year)  cl(provdist)  ffirst
eststo: ivreghdfe non_FDI_jobs1  ( `x'= ivCEMPRE_census )  , absorb(provdist  year)  cl(provdist)  ffirst
eststo: ivreghdfe formal_jobs1   (`x'= ivCEMPRE_census )  , absorb(provdist  year)  cl(provdist) ffirst
eststo: ivreghdfe informal_jobs1 (  `x'= ivCEMPRE_census )  , absorb(provdist  year)  cl(provdist )  ffirst
esttab using "city`x'_instrument_card_plus.tex", replace label nogaps se b(%9.3f) star(* 0.10 ** 0.05 *** 0.01) stats(N r2 widstat, fmt(%9.0g %9.2f %9.2f %9.2f %9.2f) labels(N "R-sq" "F IV")) nonote
}

****************************************************
****************************************************

****************************************************
****************************************************
*FIGURE A.10 Panel C
****************************************************
****************************************************

drop residz
reghdfe ivCEMPRE_census     , absorb(provdist  year) resid nocons
predict residz, resid 

*benchmark size of gamma
reg  residy residx , vce( robust )
ivreg  residy (residx = residz) ,  robust 
imperfectiv  residy (residx = residz), vce( robust )  
 matrix LRbounds = e(LRbounds)
    local lCI = LRbounds[1,1]
    local uCI = LRbounds[1,4]
    local LB  = LRbounds[1,2]
    local UB  = LRbounds[1,3]

reghdfe FDI_stock_jobs    ivCEMPRE_census     , absorb(provdist  year)  nocons
local mval = 3* _b[ivCEMPRE_census]
    foreach num of numlist 0(1)10 {
        local om`num' = ((`num'/10)*`mval'/sqrt(12))^2
        local mu`num' = (`num'/10)*`mval'/2
        local d`num' = (`num'/10)*`mval'
    }
    local mvar = (`mval'/sqrt(12))^2
   
    plausexog ltz residy (residx = residz),     omega(`mvar') mu(0) level(0.95) graph(residx) ///
    graphomega(`om0' `om1' `om2' `om3' `om4' `om5' `om6' `om7' `om8' `om9' `om10') ///
    graphmu(`mu0' `mu1' `mu2' `mu3' `mu4' `mu5' `mu6' `mu7' `mu8' `mu9' `mu10') ///
    graphdelta(`d0' `d1' `d2' `d3' `d4' `d5' `d6' `d7' `d8' `d9' `d10')  ///
    ytitle(IV estimate) xtitle({&delta}) vce(robust) scheme(Plotplainblind)  legend(off) 

graph export "IVrobust1_Remix_REStat_card_plus_.pdf", as(pdf) replace


****************************************************
****************************************************

****************************************************
****************************************************
*TABLE A.6
****************************************************
****************************************************

lab var total_jobs  "Total jobs"
lab var men "Men jobs"
lab var women "Women jobs"
lab var educated  "Skilled jobs"
lab var noteducated "Unskilled jobs"
lab var unemployment "Unemployment rate"
lab var participationrate "Participation rate"
lab var ivCEMPRE_census "Instrument"
lab var people "Pop (15-59)"
lab var nolf "Inactive"
lab var unemp "Unemployed"


bys year: fsum FDI_stock cum_projects FDI_stock_jobs cum_jobs total_jobs non_FDI_jobs1 informal_jobs1 formal_jobs1  ///
 men women educated noteducated  people   unemp nolf ,  stat( sum mean sd  )   uselabel 


****************************************************
****************************************************

****************************************************
****************************************************
*Table A.11 
****************************************************
****************************************************

*Panel A
foreach x in FDI_stock_jobs    {
eststo clear
eststo: ivreghdfe people    `x' , absorb(provdist  year)  cl(provdist  )  ffirst
eststo: ivreghdfe total_jobs    `x' , absorb(provdist  year)  cl(provdist  )  ffirst
eststo: ivreghdfe  unemp    `x'  , absorb(provdist  year)  cl(provdist  )  ffirst
eststo: ivreghdfe  nolf   `x'  , absorb(provdist  year)  cl(provdist  )  ffirst
esttab using citymain`x'.tex, replace label nogaps se b(%9.3f) star(* 0.10 ** 0.05 *** 0.01) stats(N r2 widstat, fmt(%9.0g %9.2f %9.2f %9.2f %9.2f) labels(N "R-sq" "F IV")) nonote
}
*Panel C
foreach x in FDI_stock_jobs    {
eststo clear
eststo: reghdfe men    `x' , absorb(provdist  year)  cl(provdist  )  nocons
eststo: reghdfe  women   `x' , absorb(provdist  year)  cl(provdist  )  nocons
eststo: reghdfe  educated    `x'  , absorb(provdist  year)  cl(provdist  )   nocons
eststo: reghdfe  noteducated    `x'  , absorb(provdist  year)  cl(provdist  )  nocons
esttab using gender_jobs_ols_`x'.tex, replace label nogaps se b(%9.3f) star(* 0.10 ** 0.05 *** 0.01) stats(N r2, fmt(%9.0g %9.2f %9.2f %9.2f %9.2f) labels(N "R-sq")) nonote
}
*Panel B
use clean_hh_firms_FDI_ano2, clear
drop provdist
gen provdist=100*province+district
collapse (sum) FDI_stock_jobs,  by(provdist year)
merge 1:1 provdist year using HH_ruralurban.dta
egen time=group(year)
xtset provdist time
lab var FDI_stock_jobs "FDI jobs (CEMPRE)"
lab var urbanworkers "Urban jobs"
lab var ruralworkers "Rural jobs"
lab var urbanpop "Urban pop (15-59)"
lab var ruralpop "Rural pop (15-59)"
eststo clear
eststo: reghdfe urbanpop FDI_stock_jobs    , absorb(provdist year) cl(provdist) nocons
eststo: reghdfe ruralpop FDI_stock_jobs    , absorb(provdist year) cl(provdist) nocons
eststo: reghdfe urbanworkers  FDI_stock_jobs   , absorb(provdist year) cl(provdist) nocons
eststo: reghdfe ruralworkers FDI_stock_jobs    , absorb(provdist year) cl(provdist) nocons
esttab using citymainFDI_stock_jobs3.tex, replace label nogaps se b(%9.3f) star(* 0.10 ** 0.05 *** 0.01) stats(N r2, fmt(%9.0g %9.2f %9.2f %9.2f %9.2f) labels(N "R-sq")) nonote


****************************************************
****************************************************

****************************************************
****************************************************
*FIGURE A.12
****************************************************
****************************************************


use clean_hh_firms_FDI_micro_ano.dta,replace
 
replace FDI_stock=0 if alternativesector==-1
 
gen employment=0
replace employment=1 if alternativesector!=-1
gen age2=age^2

lab var gender "Man"
lab var age "Age"
lab var age2 "Age$^2$"
lab var employment "Employment"
lab var educationlevel_bi "Tertiary Education"

reg  employment educationlevel_bi##gender##c.FDI_stock age age2 [pw=weight]  if year>2008 ,   cl(provdist )
margins, dydx(FDI_stock) over( gender  educationlevel_bi)
marginsplot, yline(0)    ytitle("") csort  scheme(Plotplainblind) xsize(4) ysize(4) plotop(lcolor(white)) by(gender, label("Female" "Male")) ///
byopt(title("Average marginal effect of an FDI project" "on the probability of being employed")) ///
xlabel(-.5(.5)1.5) title("")  xtitle("") ///
xlabel (-.5 " " 0 "Unskilled" .5 " " 1 "Skilled" 1.5 " " )
graph export "mfx_employment_.pdf", as(pdf)  replace

reghdfe  lnwage educationlevel_bi##gender##c.FDI_stock age age2 [pw=weight] if year>2008 & alternativesector!=-1 ,   absorb(provdist alternativesector) 
margins, eydx(FDI_stock) over( gender  educationlevel_bi )  
marginsplot, yline(0)    ytitle("") csort  scheme(Plotplainblind) xsize(4) ysize(4) plotop(lcolor(white)) by(gender, label("Female" "Male")) ///
byopt(title("Percent increase in wages" "associated with an FDI project")) ///
xlabel(-.5(.5)1.5) title("") xtitle("") ///
xlabel (-.5 " " 0 "Unskilled" .5 " " 1 "Skilled" 1.5 " " )
graph export "mfx_wage_.pdf", as(pdf)  replace



****************************************************
****************************************************

****************************************************
****************************************************
*THE END
****************************************************
****************************************************




